如何在 NodeJS 上使用 res.cookie(key , value) 设置多个 cookie?

新手上路,请多包涵

我在 NodeJS 上使用 cookie,我想知道如何设置多个 cookie 以在客户端上发送。

我努力了 :

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)

似乎没有任何作用。出了什么问题?任何提示都会很棒,

谢谢

原文由 Webwoman 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 664
2 个回答

您只需多次调用 cookie 而无需 在它们之间调用 send 。仅在完成所有 cookie 后调用 send ,因为 send 发送响应正文,cookie 标头……好吧……在标头中。 :-)

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

原文由 T.J. Crowder 发布,翻译遵循 CC BY-SA 4.0 许可协议

您必须在调用之前设置所有 cookie res.send()

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

原文由 m1ch4ls 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏