Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove X-Powered-By in ExpressJS [duplicate]

I want to remove X-Powered-By for Security,Save Bandwidth in ExpressJS(node.js). how to do it? it could be filter(app.use) ?

app.use(function(req,res,next_cb){ /* remove X-Powered-By header */ next_cb(); }
like image 762
MajidTaheri Avatar asked Oct 19 '22 04:10

MajidTaheri


2 Answers

Don't remove it; ask Express not to generate it in the first place:

https://stackoverflow.com/a/12484642/506073

Go to your app.js and just after:

var app = express();

Add:

app.disable('x-powered-by');
like image 333
ahcox Avatar answered Oct 21 '22 17:10

ahcox


The better way to do it is:

app.disable('x-powered-by');

You can also make a middleware to remove any header like so:

app.use(function (req, res, next) {
  res.removeHeader("X-Powered-By");
  next();
});

See more info on how to remove a header:

http://nodejs.org/api/http.html#http_response_removeheader_name

like image 235
alessioalex Avatar answered Oct 21 '22 18:10

alessioalex