javascript - In Node.js ExpressConnect, is there a way to set the session to infinity? - Stack Overflow

I'm doing it like this:app.use(express.session({cookie:{domain:"."+settings.c.SITE_DOMAI

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.

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.

Share asked Jan 21, 2012 at 6:48 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges 3
  • 3 Cookies (including session cookies) cannot have infinite expiration dates. The largest you can get, practically, is a date in 2038... after that you'll overflow the timestamp field. – Dan Grossman Commented Jan 21, 2012 at 6:52
  • OK, sure that works. How do I set it to 2038? – TIMEX Commented Jan 21, 2012 at 11:44
  • 3 @TIMEX never expiring is silly. – Raynos Commented Jan 21, 2012 at 12:26
Add a ment  | 

1 Answer 1

Reset to default 4

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('wele to the session demo. refresh!');
      }
    }
  ).listen(3000);

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745045632a4608079.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信