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

11,526

Solution 1

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');

Solution 2

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');
Share:
11,526
Webwoman
Author by

Webwoman

Web enthusiast the day... supa-heroe the night

Updated on June 14, 2022

Comments

  • Webwoman
    Webwoman almost 2 years

    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