Baroque Code
Baroque Code

Reputation: 144

express-session doesn't save any value

Before I reload or move to another page, console.log(req.session) print exactly what I saved values but It's no longer. I tried req.session.save(), req.session.regenerate({..}) or used .save() and .reload() at the same time. I cannot understand why It happen. I'm struggling with this 6-hours.

app.js

var session = require('express-session');

app.use(session({
    cookie: {
        maxAge:30 * 60 * 1000,
        expires: 30* 60 * 1000,
        httpOnly: true
    },
    rolling: true,
    resave: true,
    saveUninitialized: true,
    secret: '~~~~~'
}));

routes/index.js

router.get('/', function(req, res, next) {
    req.session.test = true;
    req.session.save();
    console.log(req.session);
    res.render('index');
});
router.get('/reg', function(req, res){
    console.log(req.session);
    res.render('another');
}

When I access 'localhost:3000' the console print like this

Session {
  cookie: 
   { path: '/',
     _expires: 1800000,
     originalMaxAge: 1800000,
     httpOnly: true },
  test: true }

But If I go to 'localhost:3000/reg', It's gone.

Session {
  cookie: 
   { path: '/',
     _expires: 1800000,
     originalMaxAge: 1800000,
     httpOnly: true } }

package ver.

Upvotes: 1

Views: 1130

Answers (1)

robertklep
robertklep

Reputation: 203231

As per the documentation, "The expires option should not be set directly; instead only use the maxAge option".

If you remove the expires property, it should work.

Upvotes: 2

Related Questions