简体   繁体   English

Node.js - writeHead设置“Location”和“Set-Cookie”

[英]Node.js - writeHead to set both “Location” and “Set-Cookie”

In node.js(running this in Connect.js), I am able to set ether the Location or Set-Cookie with writehead, but not both at the same time. 在node.js中(在Connect.js中运行),我可以使用writehead设置ether或者Set-Cookie,但不能同时设置两者。 Currently, the below sets the cooke but the URL does not get redirected to the new location: 目前,下面设置了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() }
}

On a side note, I am doing this for the learning experience and do not want to use any pre-written plugins. 在旁注中,我这样做是为了学习体验,并且不想使用任何预先编写的插件。

Response#writeHead expects the headers to be an Object not a list of array arguments. Response#writeHead期望标头是Object而不是数组参数列表。

The Node HTTP documentation has the following signature defined: Node HTTP文档定义了以下签名:

response.writeHead(statusCode, [reasonPhrase], [headers])

If you want to pass in multiple headers your code from above should read: 如果您想传入多个标头,则上面的代码应为:

response.writeHead(302, {
    Location: 'https://staging.dx.host.com' + req.url',
    'Set-Cookie': 'fake-token=5b49adaaf7687fa'
});

The [] on the reasonPhrase means its optional and infers what you've provided to the function based on the types of the arguments. reasonPhrase上的[]表示它是可选的,并根据参数的类型推断您为函数提供的内容。

Also, you don't need to wrap the key part of an object in quotes unless it contains characters that are invalid for variable names (like the - .) And a single ' will do for all strings -- there is no difference in javascript between ' and " . 此外,您不需要将对象的key部分用引号括起来,除非它包含对变量名无效的字符(如- 。)并且单个'将对所有字符串执行 - 在javascript中没有区别之间的'"

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

相关问题 将set-cookie标头发送到node.js中的重定向网址 - Send a set-cookie header to a redirect url in node.js 在Node.js中,如何在 <meta http-equiv> 标签 - in Node.js, how to get set-cookie value in a <meta http-equiv> tag node.js socket.io-client是否应该处理set-cookie - Is node.js socket.io-client supposed to handle set-cookie Cookie设置在位置“ / set-cookie”,但无法使用express.js在“ / user”处检索。 - A cookie is set at location `/set-cookie` but cannot be retrieved at `/user` using express.js 使用Node.js设置HTTP标头Cookie - Set HTTP Header Cookie with Node.js 在Node.js中设置cookie值 - Set a cookie value in Node.js res.setHeader(“ Set-Cookie”,…)未在Node / Express中设置cookie - res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express 如何使用 res.writeHead(301, { &quot;Location&quot;: &quot;/path/within/site&quot; }) 在 http node.js 的 res.writehead 中附加警报消息? - How to attach alert message in res.writehead in http node.js with res.writeHead(301, { "Location": "/path/within/site" })? 节点 js express-session Set-Cookie on Postman/不在 Flutter http 标头中 - node js express-session Set-Cookie on Postman/ Not in Flutter http headers Set-Cookie header 中的 Cookie 未设置 - Cookie in Set-Cookie header not being set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM