【问题标题】:In Node.js Express/Connect, is there a way to set the session to infinity?在 Node.js Express/Connect 中,有没有办法将会话设置为无穷大?
【发布时间】:2012-02-15 14:23:44
【问题描述】:

我是这样做的:

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

当我登录我的redis并输入TTL sess:...时,这个会话似乎已经过期了。

如何使会话永不过期? (一切)。我还希望 cookie 永不过期。

【问题讨论】:

  • Cookie(包括会话 Cookie)不能有无限的过期日期。实际上,您可以获得的最大日期是 2038 年的日期...之后您将溢出时间戳字段。
  • 好的,确定有效。如何将其设置为 2038?
  • @TIMEX 永不过期是愚蠢的。

标签: javascript session node.js cookies


【解决方案1】:

正如Connect guide on the session middleware page 中提到的(Express 内部使用 Connect),您可以在会话中指定 maxAge 选项:

  • cookie Session cookie 设置,默认为 { path: '/', httpOnly: true, maxAge: 14400000 }

例子:

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);

注意:maxAge 以毫秒为单位,例如一天 = 86400000

【讨论】:

  • 以毫秒为单位的一天实际上比86,400长,它是86,400,000毫秒长。 (1 * 24 * 60 * 60 * 1000) =&gt; (day * hours * minutes * seconds * milliseconds)
猜你喜欢
  • 2020-01-23
  • 2018-10-13
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多