[英]node.js Use a variable inside the res.writeHeads() function
Heyo在那里。 我有以下问题:
我用这个
res.writeHead(200, {
"Content-Length": template["stylecss"].length,
"Connection": "Close",
"X-XSS-Protection": "1; mode=block",
"Server": servername,
"Content-Type": "text/css"
});
将响应标头写入客户端。
但是现在,我想更改以上代码以提供预定义的标头。 像这样:
var defresheads = {“ X-Frame-Options”:“拒绝”,“ X-Powered-By”:服务器名};
res.writeHead(200, {
defresheads,
"Content-Length": template["stylecss"].length,
"Connection": "Close",
"X-XSS-Protection": "1; mode=block",
"Server": servername,
"Content-Type": "text/css"
});
现在,当我运行脚本时,它显示以下内容:
/home/dontrm/dontrm.js:47
defresheads,
^
SyntaxError: Unexpected token ,
还有另一种方法吗?
使用辅助函数来连接标题对象,例如
function jsonConcat(o1, o2) {
for (var key in o2) {
o1[key] = o2[key];
}
return o1;
}
然后您可以按以下方式使用它:
var defresheads = { "X-Frame-Options": "deny", "X-Powered-By": servername };
res.writeHead(200, jsonConcat(defresheads, {
"Content-Length": template["stylecss"].length,
"Connection": "Close",
"X-XSS-Protection": "1; mode=block",
"Server": servername,
"Content-Type": "text/css"
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.