how to remove X-Powered-By in ExpressJS

Httpnode.jsHttp HeadersExpress

Http Problem Overview


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(); }

Http Solutions


Solution 1 - Http

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

Solution 2 - Http

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

Solution 3 - Http

Middleware snippet from: https://stackoverflow.com/questions/5867199/cant-get-rid-of-header-x-powered-byexpress/13112545#13112545

function customHeaders( req, res, next ){
  // Switch off the default 'X-Powered-By: Express' header
  app.disable( 'x-powered-by' );

  // OR set your own header here
  res.setHeader( 'X-Powered-By', 'Awesome App v0.0.1' );

  // .. other headers here

  next();
}

app.use( customHeaders );

// ... now your code goes here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMajidTaheriView Question on Stackoverflow
Solution 1 - HttpahcoxView Answer on Stackoverflow
Solution 2 - HttpalessioalexView Answer on Stackoverflow
Solution 3 - HttppapercowboyView Answer on Stackoverflow