繁体   English   中英

如何从express req对象中删除标头?

[英]How to remove a header from express req object?

我试图弄清楚如何从express中的req对象中删除标头。 我相信此res.disable("Header Name")会将其从res对象中删除,但对req.headers

可以像添加此中间件一样简单:

app.use(function(req, res, next) {
  delete req.headers['header-name']; // should be lowercase
  next();
});

您可以简单地从request对象中delete标头,就像我在下面做的那样-

console.log(req.headers)

// { host: 'localhost:8081',
//   connection: 'keep-alive',
//   auth_token: 'c79d2f80029c1a1382b2e831643e5447b902a6f9',
//   'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36',
//   'postman-token': 'b2cef620-f85d-556c-acc1-8337da2d5e81',
//   'cache-control': 'no-cache',
//   api_key: 'FB499A4FF77901AFCD2278457658B7F7B17EAC112B489DAA304D3F2A059DFCC4',
//   'content-type': 'application/json',
//   accept: '*/*',
//   dnt: '1',
//   'accept-encoding': 'gzip, deflate, sdch, br',
//   'accept-language': 'en-US,en;q=0.8' }

// Now Delete the headers from your request object.

delete req.headers;

console.log(req.headers) // undefined

如果要从标题中删除任何键,请使用以下代码:

delete req.headers['auth_token'];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM