Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js/Express, how do I automatically add this header to every "render" response?

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?

like image 874
TIMEX Avatar asked Jul 12 '11 08:07

TIMEX


People also ask

How do I add a header in node JS?

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.

What is content type header in node JS?

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");

How do you send data as response in node JS?

Methods to send response from server to client are:Using send() function. Using json() function.

What does res render do in node JS?

The res. render() function is used to render a view and sends the rendered HTML string to the client.


2 Answers

You probably want to use app.use with your own middleware:

app.use(function(req, res, next) {     res.header('X-XSS-Protection', 0);     next(); }); 
like image 102
Francesc Rosas Avatar answered Sep 30 '22 03:09

Francesc Rosas


// 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.

like image 39
BGerrissen Avatar answered Sep 30 '22 02:09

BGerrissen