我正在尝试找出如何从 Express 中的
req
对象中删除标头。我相信这 res.disable("Header Name")
会将其从 res 对象中删除,但同样不适用于 req.headers
这可能就像添加这个中间件一样简单:
app.use(function(req, res, next) {
delete req.headers['header-name']; // should be lowercase
next();
});
您可以简单地从您的
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'];
只是想我会提到在node/express 中标头键会小写。
所以这对我来说不起作用:
delete req.headers['Authorization']
但这对我有用:
delete req.headers['authorization']