简体   繁体   English

在Node.js Express / Connect中,有没有办法将会话设置为无穷大?

[英]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. 当我登录我的redis并输入TTL sess:... ,似乎此会话已过期。

How can I make the sessions never expire? 如何让会话永不过期? (for everything). (对于一切)。 I also want the cookies to never expire. 我也希望cookie永不过期。

As mentioned in the Connect guide on the session middleware page (Express uses Connect internally), you can specify a maxAge option on sessions: 会话中间件页面上Connect指南 (Express在内部使用Connect)中所述,您可以在会话上指定maxAge选项:

  • cookie Session cookie settings, defaulting to { path: '/', httpOnly: true, maxAge: 14400000 } cookie会话cookie设置,默认为{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 注意:maxAge以毫秒为单位,因此例如一天= 86400000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM