Heyo out there. I have the following problem:
I use this
res.writeHead(200, {
"Content-Length": template["stylecss"].length,
"Connection": "Close",
"X-XSS-Protection": "1; mode=block",
"Server": servername,
"Content-Type": "text/css"
});
to write the response headers to a client.
But now, i want to change the above code to serve predefined headers. Something like this:
var defresheads = { "X-Frame-Options": "deny", "X-Powered-By": servername };
res.writeHead(200, {
defresheads,
"Content-Length": template["stylecss"].length,
"Connection": "Close",
"X-XSS-Protection": "1; mode=block",
"Server": servername,
"Content-Type": "text/css"
});
Now, when i run the script, it says the following:
/home/dontrm/dontrm.js:47
defresheads,
^
SyntaxError: Unexpected token ,
Is there another way to do this?
Use a helper function to concat your header objects, like
function jsonConcat(o1, o2) {
for (var key in o2) {
o1[key] = o2[key];
}
return o1;
}
And then you can use this as follows:
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"
}));