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

我想删除X-Powered-By for Security,在ExpressJS(node.js)中保存带宽。 怎么做? 它可能是filter(app.use)?

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

制作一个这样的中间件:

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

查看有关如何删除标题的更多信息:

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

更新:

那时我还没有意识到有更好的办法可以做到这一点 (如许多人所build议的那样):

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

不要删除它; 请明确表示不要生成它:

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

转到您的app.js之后:

 var app = express(); 

加:

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

中间件片段来自: 无法摆脱标题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 

这已经在这里回答: 无法摆脱标题X-Powered-By:Express

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

🙂