Wednesday 20 February 2019

A method to access express session variables in the front end

I have been using express session variables for a small project, however, I have only been able to get express session variables into the page using templating. I would like javascript on the frontend to be able to access this data and serve the user dynamically. I have looked through document.cookie, sessionStorage and localStorage thinking I would find something(which I didn't) since I am new to handling sessions and cookies.One of the ways I thought of a way around this, is to have a route which would provide the user with the session details, let us say /api/getsession or something among those lines with fairly simple backend route code:Installed and used packages:ExpressExpress SessionBody-parser...A few others not required for this thread​app.get("/api/getsession",(req,res)=>{ res.json(req.session) }) However, since this would require the page on the front end to perform an http request, it might add onto the loading time of the page. Thus, if there is a way to access the session variables on the page itself, IMO it would be more efficient.​Therefore if there is something wrong in the technique I am using or a better technique/package I could use, kindly point this out as I am new to node. Appreciate the help :)​

Submitted February 20, 2019 at 08:01AM by xZakurax

No comments:

Post a Comment