如何在ExpressJS中删除X-Powered-By

时间:2012-05-23 10:05:01

标签: http node.js http-headers express

我想在ExpressJS(node.js)中删除X-Powered-By for Security,Save Bandwidth。怎么做? 它可能是过滤器(app.use)?

app.use(function(req,res,next_cb){ /* remove X-Powered-By header */ next_cb(); }

4 个答案:

答案 0 :(得分:203)

不要将其删除;要求Express首先不要生成它:

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

转到app.js,然后转到:

var app = express();

添加:

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

答案 1 :(得分:110)

更好的方法是:

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

您还可以制作中间件以删除任何标题,如下所示:

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

查看有关如何删除标题的详细信息:

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

答案 2 :(得分:5)

来自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

答案 3 :(得分:-1)

这已在这里得到解答:Can't get rid of header X-Powered-By:Express

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

:)