简体   繁体   English

无法使用Express 3.0在Node.js中设置cookie'expires'或'maxAge'

[英]Can't set cookie 'expires' or 'maxAge' in Node.js using Express 3.0

I've been trying to set my cookie's expiry date in Node.js using Express 3.0, but nothing is working. 我一直在尝试使用Express 3.0在Node.js中设置我的cookie的到期日期,但没有任何工作。

My first attempt: 我的第一次尝试:

res.cookie('user', user, { maxAge: 9000, httpOnly: true });

Just ends in a cookie that has an invalid expiry time according to Chrome. 根据Chrome,只是以cookie无效,到期时间结束。 Then I tried to set 'expires' instead, like so: 然后我尝试设置'expires',如下所示:

res.cookie('user', user, { expires: new Date(new Date().getTime()+5*60*1000), httpOnly: true });

And now my cookie is just a session cookie. 现在我的cookie只是一个会话cookie。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

You have to use req.session.cookie : 你必须使用req.session.cookie

req.session.cookie.expires = false;

req.session.cookie.maxAge = 5 * 60 * 1000;

See also connect docs . 另请参阅连接文档

请注意maxAge以毫秒为单位 -

res.cookie('jwtToken', token, { maxAge: 2 * 60 * 60 * 1000, httpOnly: true }); // maxAge: 2 hours

The accepted answer doesn't work for me.. but the original question has the version that does work. 接受的答案对我不起作用..但原始问题的版本确实有效。 For example 例如

var language = 'en';
//10 * 365 * 24 * 60 * 60 * 1000 === 315360000000, or 10 years in milliseconds
var expiryDate = new Date(Number(new Date()) + 315360000000); 
this.__res.cookie('lang', language, { expires: expiryDate, httpOnly: true });

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

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