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

2019-03-08 12:42发布

This question already has an answer here:

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

4条回答
beautiful°
2楼-- · 2019-03-08 12:57

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

查看更多
干净又极端
3楼-- · 2019-03-08 13:02

Middleware snippet from: Can't get rid of header X-Powered-By:Express

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
查看更多
We Are One
4楼-- · 2019-03-08 13:07

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');
查看更多
别忘想泡老子
5楼-- · 2019-03-08 13:09

This was already answered here: Can't get rid of header X-Powered-By:Express

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

:)

查看更多
登录 后发表回答