mercredi 5 octobre 2016

Using express-sessions: unable to get variable

Vote count: 0

I am new to using express-sessions and I'm unsure of what I'm doing wrong and/or not understanding about how to use express-sessions. Essentially I want to pass information that will have state as a user navigates to different routes, and I believe that I can use express-session for this, correct?

In the example below (two separate route js files) I have a req.session variable set, and when the user goes to a different route I wanted to pull up that req.session variable. However, req.session doesn't show the variable. Hopefully someone can help, your input is appreciated!

login.js route

...
let session = require('express-session');
app.use(session({
    secret: "why is this not working",
    resave: true,
    saveUnintialized: true,
    cookie: { maxAge: 60000 }
  }));

router.get('/myUrl', function(req, res) {
  req.session.test = 'true';
  res.render('/myForm.jade');
});
...

user.js route

...
let session = require('express-session');
app.use(session({
    secret: "why is this not working",
    resave: true,
    saveUnintialized: true,
    cookie: { maxAge: 60000 }
  }));
router.post('/login', function(req, res, next) {
console.log('req.session.test: '+req.session.test); 
console.log(req.session);
...

user.js output:

req.session.test: undefined

Session {
  cookie: 
   { path: '/',
     _expires: 2016-10-05T19:40:37.291Z,
     originalMaxAge: 60000,
     httpOnly: true } }

asked 8 secs ago

Let's block ads! (Why?)



Using express-sessions: unable to get variable

Aucun commentaire:

Enregistrer un commentaire