简体   繁体   English

Chrome仅设置“ Set-Cookie”标题中的第一个Cookie

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

I have a dead simple http nodejs server: 我有一个死的简单http nodejs服务器:

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');
}).listen(9999);

Upon visiting the page in the latest version of Chrome, these are the Response Headers sent from the server: 在最新版本的Chrome中访问该页面时,这些是服务器发送的响应标头:

服务器响应头

So, the server sends the correct cookies. 因此,服务器发送正确的cookie。 However, the browser only seems to store the first one ( age=44 ). 但是,浏览器似乎只存储第一个( age=44 )。 If I refresh the page again, these are the Request Headers being sent to the server: 如果我再次刷新页面,则这些是发送到服务器的请求标头:

服务器请求标头

Only the first cookie ( age=44 ) is sent. 仅发送第一个cookie( age=44 )。 Logging document.cookie in the console also returns just 'age=44' . 在控制台中记录document.cookie也会返回'age=44'

When inspecting the cookies from the Chrome's UI, I can also see that only the first one is saved: 从Chrome的用户界面检查Cookie时,我还看到仅保存了第一个Cookie:

What's the problem here? 这是什么问题

chrome ui饼干

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(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]) : 如果使用Express 4.11.0+,则可以使用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=/');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM