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
The Set-Cookie HTTP response header is used to send a cookie from the server to the user agent, so that the user agent can send it back to the server later. To send multiple cookies, multiple Set-Cookie headers should be sent in the same response.
To set more than one, you just set document. cookie more than once. The ; separator is used to specify the additional info, not to add more different cookies.
When the user agent generates an HTTP request, the user agent MUST NOT attach more than one Cookie header field. It looks like the use of multiple Cookie headers is, in fact, prohibited!
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With