Cookie problem. I can't set the maxAge nor the expires time.

5,467 views
Skip to first unread message

murvinlai

unread,
Apr 28, 2011, 10:20:58 PM4/28/11
to Express
I have tested many times...
when I do this:
res.cookie('remember', '1', { path: '/', httpOnly: true });

I can set the cookie

but when I do this (from example), I can't.

res.cookie('remember', '1', { path: '/', expires: new Date(Date.now()
+ 900000), httpOnly: true });

nor I try this

res.cookie('remember', '1', { path: '/', maxAge:999999, httpOnly:
true });


As soon as I remove expires or maxAge, it works.

TJ Holowaychuk

unread,
Apr 29, 2011, 12:06:49 PM4/29/11
to expre...@googlegroups.com
hm.. doesn't sound right. Will try and look into it soon

-- 
TJ Holowaychuk
--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

TJ Holowaychuk

unread,
Apr 29, 2011, 12:09:23 PM4/29/11
to expre...@googlegroups.com
could be right, chrome (With maxAge resolving to expires) mentions that it's a session cookie. digging in some more

-- 
TJ Holowaychuk

TJ Holowaychuk

unread,
Apr 29, 2011, 12:13:40 PM4/29/11
to expre...@googlegroups.com
nevermind, chrome is just being stupid, they work fine for me

-- 
TJ Holowaychuk

Murvin Lai

unread,
Apr 29, 2011, 1:49:52 PM4/29/11
to expre...@googlegroups.com
I'm using Firefox 4 and doesn't set the cookie.  Forgot to mention about it.  

Murvin Lai

unread,
Apr 29, 2011, 2:25:17 PM4/29/11
to expre...@googlegroups.com
Also, I just tried on IE.  it won't set the cookie even the simplest form:
res.cookie('test', 'dfasfasdf');

but the same code can set the cookie in FX / Chrome.  

and for the expiry date / maxAge, it doesn' work in both FX & Chrome. :(

Murvin

TJ Holowaychuk

unread,
Apr 29, 2011, 2:26:51 PM4/29/11
to expre...@googlegroups.com
weird :s IE doesn't surprise me haha.. unfortunately I cannot test IE since I don't have a VM set up

-- 
TJ Holowaychuk

Murvin Lai

unread,
Apr 29, 2011, 3:00:49 PM4/29/11
to expre...@googlegroups.com
I have just run another test, using the low level writeHead function.   here is what I found:

var response = ExpressRes.req.res;   // get the original nodejs response object.

response.writeHead(200, {
   'Set-Cookie': 'mycookie=test; expires=Sun, 15-May-2011 21:47:38 GMT; path=/;  ',
   
   'Content-Type': 'text/plain'
 });
 response.end('Hello World\n');

For this with expiration date, it works in both FireFox & Chrome.  So, I believe there is a bug in Express when writing the expires date in cookie. :)

However, even with this default node.js way to write header, I cannot set the cookie in IE (regardless with expiry or not).  so, this IE issue is another problem in which I will report to the node.js creator. :)

Thanks TJ.  Please let me know if there is fix for the cookie expiration date.  and I will let you know if I found any thing regarding IE issue. :-)  

TJ Holowaychuk

unread,
Apr 29, 2011, 3:06:07 PM4/29/11
to expre...@googlegroups.com
toUTCString() should be fine. node has nothing to do with cookies so as long as the header field is set properly it should be fine

-- 
TJ Holowaychuk

On Friday, April 29, 2011 at 12:03 PM, TJ Holowaychuk wrote:

AFAIK toU

-- 
TJ Holowaychuk

Murvin Lai

unread,
Apr 29, 2011, 3:14:59 PM4/29/11
to expre...@googlegroups.com
Hmmm. sorry. what doyou mean by toUTCString()   ?

TJ Holowaychuk

unread,
Apr 29, 2011, 3:21:31 PM4/29/11
to expre...@googlegroups.com
Date#toUTCString()

-- 
TJ Holowaychuk

Murvin Lai

unread,
Apr 29, 2011, 5:50:36 PM4/29/11
to expre...@googlegroups.com
thanks.

after more testing, here is what I found:

it works in IE.   just that somehow IE doesn't show me the cooke in the "view files".  but I look at request header, the cookies are there.

for setting expiry date, the example doesn't work:

res.cookie('remember', '1', { path: '/', expires: new Date(Date.now() + 900000), httpOnly: true });

but, if I change to this, it works:
var exdate=new Date(); exdate.setDate(exdate.getDate() + 10);
res.cookie('remember', '1', { path: '/', expires: exdate, httpOnly: true });

and works on all browsers. :)

Herman A. Junge [neoSource]

unread,
Apr 29, 2011, 6:05:22 PM4/29/11
to expre...@googlegroups.com
My two cents about cookies:

Cookies have these problems sometimes. I don’t know you guys, but I work
with Chrome, and when I started studying sessions, I couldn’t find the
cookies storing the ‘.sid value’ either!

Fortunately Chrome debugger (Ctrl + Alt + I) helps a lot: You go to the
network tab and You can find the headers and cookies there.

Another advice is to take a proxy, in case you want to study the headers and
cookies and not miss anything.

Herman A. Junge
www.neosource.cl

Murvin Lai

unread,
Apr 29, 2011, 6:50:24 PM4/29/11
to expre...@googlegroups.com
or use Fiddler to see all the request header and response body to confirm if the cookie is there. 
Reply all
Reply to author
Forward
0 new messages