I've been trying to set my cookie's expiry date in Node.js using Express 3.0, but nothing is working.
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. Then I tried to set 'expires' instead, like so:
res.cookie('user', user, { expires: new Date(new Date().getTime()+5*60*1000), httpOnly: true });
And now my cookie is just a session cookie.
Does anyone know how to fix this?
You have to use req.session.cookie:
req.session.cookie.expires = false;
req.session.cookie.maxAge = 5 * 60 * 1000;
See also connect docs.
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 });