开发者

How can I set a cookie's expiration in NodeJS?

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开发者_StackOverflow社区 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


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.

response.writeHead(200, {
    'Set-Cookie':'sesh=wakadoo; expires='+new Date(new Date().getTime()+86409000).toUTCString()
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜