Bogdan M. Bogdan M. - 1 year ago 85
HTTP Question

Chrome only sets the first cookie from the 'Set-Cookie' header

I have a dead simple http nodejs server:

require('http').createServer(function(req, res) {
res.statusCode = 200;
res.setHeader('Set-Cookie', 'age=44; name=ok; something=else; path=/');
res.end('ok ok ok', 'utf8');

Upon visiting the page in the latest version of Chrome, these are the Response Headers sent from the server:

server response headers

So, the server sends the correct cookies. However, the browser only seems to store the first one (
). If I refresh the page again, these are the Request Headers being sent to the server:

server request headers

Only the first cookie (
) is sent. Logging
in the console also returns just

When inspecting the cookies from the Chrome's UI, I can also see that only the first one is saved:

What's the problem here?

chrome ui cookies

Answer Source

If you're just using vanilla Node then you can simply pass an array as the second parameter to res.setHeader(field, value):

res.setHeader('Set-Cookie', [
  'age=44; path=/',
  'name=ok; path=/',
  'something=else; path=/'

If using Express 4.11.0+ you can use res.append(field [, value]):

res.append('Set-Cookie', 'age=44; path=/');
res.append('Set-Cookie', 'name=ok; path=/');
res.append('Set-Cookie', 'something=else; path=/');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download