Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js Express/Connect, is there a way to set the session to infinity?

I'm doing it like this:

app.use(express.session({
                cookie:{domain:"."+settings.c.SITE_DOMAIN}, 
                secret:'abc',
                store: redis_store,
                }));

When I log into my redis and type TTL sess:..., it seems that there is an expiration on this session.

How can I make the sessions never expire? (for everything). I also want the cookies to never expire.

like image 306
TIMEX Avatar asked Oct 09 '22 22:10

TIMEX


1 Answers

As mentioned in the Connect guide on the session middleware page (Express uses Connect internally), you can specify a maxAge option on sessions:

  • cookie Session cookie settings, defaulting to { path: '/', httpOnly: true, maxAge: 14400000 }

Example:

connect(
      connect.cookieParser()
    , connect.session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }})
    , connect.favicon()
    , function(req, res, next){
      var sess = req.session;
      if (sess.views) {
        res.setHeader('Content-Type', 'text/html');
        res.write('<p>views: ' + sess.views + '</p>');
        res.write('<p>expires in: ' + (sess.cookie.maxAge / 1000) + 's</p>');
        res.end();
        sess.views++;
      } else {
        sess.views = 1;
        res.end('welcome to the session demo. refresh!');
      }
    }
  ).listen(3000);

Note: maxAge is in milliseconds, so for example a day = 86400000

like image 131
alessioalex Avatar answered Oct 12 '22 09:10

alessioalex