如何在 NodeJS 中设置 cookie 的过期时间?
我正在编写自己的 cookie,作为我使用 NodeJS 制作的网络服务器的一部分(即不使用任何包)。我已经能够使用类似的方法很好地做到这一点:
response.writeHead(200, {
'Set-Cookie':'sesh=wakadoo'
});
但是,我想让我的cookie比现在更持久(也就是说根本不)。我尝试过做这样的事情:
var curdate = new Date();
response.writeHead(200, {
'Set-Cookie':'sesh=wakadoo; expires='+curdate.toUTCString()
});
但这似乎不起作用。 chrome 只是忽略我设置的过期时间,而 Firefox 从不加载页面。我当前的目标是简单地给我的 cookie 一个可以由浏览器解释的过期日期...有人知道如何做到这一点吗?
最好的,
萨米
I'm writing my own cookies as part of a webserver I'm making using NodeJS (i.e. not using any packages). I've been able to do this just fine using something like:
response.writeHead(200, {
'Set-Cookie':'sesh=wakadoo'
});
However, I'd like to make my cookies more persistent than they currently are (which is to say not at all). I've tried doing something like this:
var curdate = new Date();
response.writeHead(200, {
'Set-Cookie':'sesh=wakadoo; expires='+curdate.toUTCString()
});
but alas this doesn't seem to work. chrome just ignores the expiration i set, and firefox never loads the page. My current goal is to simply give my cookie an expiration date that can be interpreted by the browser... does anybody know how to do this?
Best,
Sami
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试给过期时间一些提前时间,比如一天。客户端/服务器可能无法读取它,因为它在您提交时已过期。
Try giving the expires some lead time, like an day. The client/server may not read it because it's expired when you submit it.