Information about the server access session on the client without a reboot or additional request

I authenticate the server side using express.session . The usual way to transmit session information on the client side is to resubmit the page with the newly created HTML server version, but this requires a reboot.

I have a single page application and you want to avoid a reboot. Is there a way to access information about the connected client side of the user? Is this information stored in a cookie? How can I access it without reloading the page or requesting an additional server?

+6
source share
2 answers

The only way to do this is to either include the information in the server’s original response (possibly a dynamically created JS object embedded in HTML), or make a second request suggested by alessioalex.

It is impossible to get information from the server WITHOUT communicating with the server ...

+3
source

As suggested by @stagas, you can create your own route using Express, such as /user and return session data (user data) using JSON. This way you do not need to refresh the page.

Example:

 app.get('/user', function (req, res) { res.json(req.session.user); }); 
+3
source

Source: https://habr.com/ru/post/905403/


All Articles