在node.js(在Connect.js中运行)中,我可以用writehead设置Location或Set-Cookie,但不能同时设置两者。当前,以下内容设置了Cooke,但URL不会重定向到新位置:
function foo(req, res, next) {
var url = /container-templates/;
if (url.test(req.url)) {
console.log(url.test(req.url));
console.log(req.url);
res.writeHead(302, ["Location", 'https://staging.dx.host.com' + req.url],
["Set-Cookie", "fake-token=5b49adaaf7687fa"]);
res.end();
} else { next() }
}
附带说明一下,我这样做是为了获得学习经验,并且不想使用任何预先编写的插件。
Response#writeHead
期望标头是一个Object,而不是数组参数的列表。
该节点HTTP文档具有以下特征定义:
response.writeHead(statusCode, [reasonPhrase], [headers])
如果要传递多个标头,则上面的代码应为:
response.writeHead(302, {
Location: 'https://staging.dx.host.com' + req.url',
'Set-Cookie': 'fake-token=5b49adaaf7687fa'
});
在[]
对reasonPhrase
你所提供的基于类型的参数的函数手段其可选的和推断。
另外,key
除非对象包含对变量名无效的字符(例如-
。),否则不需要用引号将其部分引起来,并且单个'
字符将适用于所有字符串-'
和之间的javascript没有区别"
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句