How set multiple cookies with res.cookie(key , value) on NodeJS?

Viewed 14204

I'm working with cookies on NodeJS and I wonder how set multiple cookies to send on client.

I have tried :

1-

     var headers = {
      Cookie: 'key1=value1; key2=value2'
  }
  res.cookie(headers)

2-

res.cookie("isStillHere" , 'yes it is !').send('Cookie is set');
res.cookie("IP" , ip).send('Cookie is set');

3-

  var setMultipleCookies =  []
 setMultipleCookies.push('key1=value1','key2=value2')
  res.cookie(setMultipleCookies)

Seems nothing works. What is going wrong ? Any hint would be great,

thanks

3 Answers

You simply call cookie more than once without calling send in between them. Call send only after you've done all the cookies, since send sends the response body, and cookie headers are...well...in the header. :-)

res.cookie("isStillHere" , 'yes it is !');
res.cookie("IP" , ip);
res.send('Cookie is set');

You have to set all cookies before you call res.send()

res.cookie("isStillHere" , 'yes it is !');
res.cookie("IP" , ip);
res.send('Cookie is set');

ShortHand (not so short), piping the functions.

res.cookie('cookie1' : { name: 'Leo' }).cookie('cookie2' : 'Teste').send()
Related