1

I am setting up a new server in the nodeJs. where Do I need to set session such that I can access session in route files?

Gaurav Gupta
  • 11
  • 1
  • 2

2 Answers2

0

If you are using express and express-session you can access to the session data in any request handler function in req.session. For example:

app.get('/', function(req, res){
  console.log(req.session);
});

For more details refer to https://github.com/expressjs/session#reqsession.

virgiliogm
  • 946
  • 7
  • 15
  • authRoutes.route('/login').get(function(req, res){ console.log(req.session); res.status(200).send("login get :"+req.session.user); }); authRoutes.route('/login').post(function (req, res) { authenticate(req.body.username, req.body.password, function(err, user){ if (user) { console.log(req.session.id); } }); }); why session ID in both post and get are different ? is there any other document or tuutorial which help to implement session in my webapp? thank you – Gaurav Gupta Jun 17 '19 at 03:00
0

Install express-session module

npm install express-session --save

In index.js file

app.use(session({ secret: 'access_token', cookie: { maxAge: 60000 }}))

Import all routes in index.js(entry point)

app.get("/", routes);

You can access session with req.session in your function