我正在开发一个Web应用程序并使用Google Chrome 60.0.3112.113进行测试。
为了简化开发过程,我使用带有http-proxy-middleware的node.js开发Web服务器将我的API请求代理到后端。
现在,当我使用axios向其中一个API端点发送HTTP POST请求以在我的后端创建会话时,我收到了稍微更改的响应标头(从DevTools复制):
直接反应HTTP/1.1 200 OK
Content-Length: 122
Content-Type: application/json
Set-Cookie: sessionid={4621f755-37da-41da-bdbd-9a6ce0ee02b7}; Max-Age=31536000; Version=1
代理响应
HTTP/1.1 200 OK
X-Powered-By: Express
connection: close
content-length: 122
content-type: application/json
set-cookie: sessionid={4621f755-37da-41da-bdbd-9a6ce0ee02b7}; Max-Age=31536000; Version=1
Date: Thu, 07 Sep 2017 11:06:43 GMT
问题是chrome没有设置代理响应中指定的cookie(DevTools-> Application-> Storage-> Cookies保持为空),但直接响应会按预期设置cookie。 / p>
Cookie在DevTools-> Network-> 我的请求 - > Cookie中正确显示。
通过http://localhost:[8080 / 3000]
可以在chrome中忽略小写的set-cookie
标题吗?
或者其他标题是否会干扰cookie的设置?
顺便说一下:在Safari 10.1.2(12603.3.8)中正常工作
答案 0 :(得分:0)
// proxy middleware options
var options = {
target: 'http://localhost:8081', // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
logLevel: "debug",
pathRewrite: {
'^/src/api/' : '/api/'
},
onProxyRes: function (proxyRes, req, res) {
if (proxyRes.headers['set-cookie'] != undefined) {
req.session['cookie'] = proxyRes.headers['set-cookie']; // must be or you will get new session for each call
req.session['proxy-cookie'] = proxyRes.headers['set-cookie']; // add to other key because cookie will be lost
}
console.log("response: " + req.session.id);
console.log(req.session);
},
onProxyReq: function (proxyReq, req, res) {
// check for whether the session be freshed
if (req.session.view)
req.session.view ++;
else
req.session.view = 1;
// use ower key to restore cookie
if (req.session['proxy-cookie'] != undefined)
proxyReq.setHeader('cookie', req.session['proxy-cookie'][0]);
console.log("request: " + req.session.id);
console.log(req.session);
}
};
答案 1 :(得分:0)
设置cookieDomainRewrite: 'localhost'