How can I make sure a session for a client on my express.js app lasts longer than a few hours? I don't want them to log in anew every time they visit my site.
如何确保客户在我的快递上有一个会话。js应用会持续几个小时吗?我不希望他们每次访问我的网站时都重新登录。
I tried to fiddle with the expires
option on the express.session
method, but that doesn't seem to have any effect.
我试着去摆弄快递上的过期期权。会话方法,但这似乎没有任何效果。
2 个解决方案
#1
3
You can use req.session.cookie.expires = aSpecificDateAndTime;
or req.session.cookie.maxAge = aNumberOfMilliseconds;
. Both are equivalent, according to the docs.
您可以使用req.session.cookie。到期= aSpecificDateAndTime;或req.session.cookie。maxAge = aNumberOfMilliseconds;。根据医生的说法,这两者是等价的。
Forgot to mention, you can also set these options when setting the middleware (i.e. express.session({secret: "whatever", cookie: {maxAge: 60000}});
忘了说,您还可以在设置中间件(例如express)时设置这些选项。会话({secret:“whatever”,cookie: {maxAge: 60000});
#2
0
cookie: {
path: '/'
,expires: false
,httpOnly: true
,domain:'.example.com'
}
Above setting gives you to have session alive until browser closes.
上面的设置允许您在浏览器关闭前保持会话活动。
Available among all pages ( path set to root ).
可以在所有页面中找到(路径设置为root)。
#1
3
You can use req.session.cookie.expires = aSpecificDateAndTime;
or req.session.cookie.maxAge = aNumberOfMilliseconds;
. Both are equivalent, according to the docs.
您可以使用req.session.cookie。到期= aSpecificDateAndTime;或req.session.cookie。maxAge = aNumberOfMilliseconds;。根据医生的说法,这两者是等价的。
Forgot to mention, you can also set these options when setting the middleware (i.e. express.session({secret: "whatever", cookie: {maxAge: 60000}});
忘了说,您还可以在设置中间件(例如express)时设置这些选项。会话({secret:“whatever”,cookie: {maxAge: 60000});
#2
0
cookie: {
path: '/'
,expires: false
,httpOnly: true
,domain:'.example.com'
}
Above setting gives you to have session alive until browser closes.
上面的设置允许您在浏览器关闭前保持会话活动。
Available among all pages ( path set to root ).
可以在所有页面中找到(路径设置为root)。