Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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

like image 338
thisissami Avatar asked Dec 22 '22 09:12

thisissami


1 Answers

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()
});
like image 177
Ryan Olds Avatar answered Dec 24 '22 02:12

Ryan Olds