Javascript 节点 + 快速会话到期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11826792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:15:56  来源:igfitidea点击:

Node + Express Session Expiration?

javascriptnode.jssessionexpress

提问by Vadim Baryshev

I have an app in express and I have a login form. I need sessions to last for 1 month do I set maxAge to a month in milliseconds.

我有一个快速应用程序,我有一个登录表单。我需要会话持续 1 个月,我是否以毫秒为单位将 maxAge 设置为一个月。

I left two computers on and logged in for 24 hours and when I came back both were logged out.

我把两台电脑都打开并登录了 24 小时,当我回来时,两台电脑都被注销了。

How do I fix this/achieve what I'm trying to do? Thanks.

我该如何解决这个问题/实现我正在尝试做的事情?谢谢。

回答by Vadim Baryshev

You can use expires attribute instead of maxAge. It takes Date object as value. Also, check session cookie exipres on client after they set. Maybe session ended by server (i.e. memcached restart).

您可以使用 expires 属性代替 maxAge。它将 Date 对象作为值。此外,请在设置后检查客户端上的会话 cookie exipres。可能会话由服务器结束(即 memcached 重启)。

Code example:

代码示例:

app.use(express.session(
  { secret: "secret", store: new MemoryStore(), expires: new Date(Date.now() + (30 * 86400 * 1000)) 
  }));

but

  app.use(express.session(
    { secret: "secret", store: new MemoryStore(), maxAge: Date.now() + (30 * 86400 * 1000) 
    }));

works fine for me too.

对我来说也很好用。

回答by Dong

maxAgemeans how long the session lasts, in ms; expiresmeans when the session gonna expire, ie: a date object

maxAge表示会话持续多长时间,以毫秒为单位; expires表示会话何时到期,即:日期对象

var hour = 3600000
req.session.cookie.expires = new Date(Date.now() + hour)

or

或者

var hour = 3600000
req.session.cookie.maxAge = hour

Documentation

文档

回答by Alex H.

The accepted answer by @Vadim Baryshev is flawed in at least the most recent express-session implementations. When your app starts up you create the session implementation and set the max age. As for the answer using expires, that is not recommended, stick with maxAge.

@Vadim Baryshev 接受的答案至少在最近的快速会话实现中存在缺陷。当您的应用程序启动时,您创建会话实现并设置最大年龄。至于使用 expires 的答案,不推荐,坚持使用 maxAge。

This means that the maxAge example as written would expire every single session a month after the server starts, and every session created in the future would be expired immediately. Until the server was restarted, there would not be any valid sessions.

这意味着所写的 maxAge 示例将在服务器启动一个月后的每个会话过期,并且将来创建的每个会话都将立即过期。在服务器重新启动之前,不会有任何有效的会话。

Instead of passing a date object, just pass the number of milliseconds to the maxAge property like so:

而不是传递日期对象,只需将毫秒数传递给 maxAge 属性,如下所示:

app.use(
  session({
    ...,
    cookie: {
      maxAge: 30 * 24 * 60 * 60 * 1000
    }
  })
);

回答by Anderson Luiz

The documentation does not recommend setting cookie.expires directly. Instead, set cookie.maxAge directly.

该文档不建议直接设置 cookie.expires。相反,直接设置 cookie.maxAge。

Express session - cookie.expires

快速会话 - cookie.expires

回答by Hoovinator

app.use(express.session({
    secret  : 'sdfsdSDFD5sf4rt4egrt4drgsdFSD4e5',
    store   : new storage({ client : conn, cleanup: false }),
    cookie  : { maxAge  : new Date(Date.now() + (60 * 1000 * 30)) }
}));