I have many of these "controllers":
app.get('/',function(req,res){ var stuff = { 'title': 'blah' }; res.render('mytemplate',stuff); });
Notice res.render? I want to add this header to every response header I make:
X-XSS-Protection: 0
How can I add that response header automatically?
setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.
Content-Type in the response is the header we can set to inform how client would interpret the data from the server. For example, if you are sending down an HTML file to the client, you should set the Content-Type to text/html, which you can with the following code: response. setHeader("Content-Type", "text/html");
Methods to send response from server to client are:Using send() function. Using json() function.
The res. render() function is used to render a view and sends the rendered HTML string to the client.
You probably want to use app.use with your own middleware:
app.use(function(req, res, next) { res.header('X-XSS-Protection', 0); next(); });
// global controller app.get('/*',function(req,res,next){ res.header('X-XSS-Protection' , 0 ); next(); // http://expressjs.com/guide.html#passing-route control });
Just make sure this is the first controller you add, order is significant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With