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

时间:2017-07-07 08:48:15

标签: javascript node.js express

我正在尝试弄清楚如何从Express中的req对象中删除标头。我相信此res.disable("Header Name")会将其从res对象中删除,但同样不适用于req.headers

2 个答案:

答案 0 :(得分:1)

这可能就像添加这个中间件一样简单:

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

答案 1 :(得分:-1)

您可以在delete对象中设置request标题,就像我在下面所做的那样 -

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