proxyReq.setHeader does not work
here is my code
const HttpProxy = require('http-proxy')
const proxy = HttpProxy.createProxyServer()
proxy.on('proxyReq', function(proxyReq, req, res) {
proxyReq.setHeader('cookie', req.headers.authorization);
console.log('proxyReq', req.headers);
});
proxy.web(ctx.req, ctx.res, pxy.proxy)
request method is GET
my target is add cookie on request header
So I did similar things and after my proxyReq.setHeader line I did a console.log(JSON.stringify(proxyReq.headers)). Guess what, it prints undefined!
And later I used more console.log line all around the place and found that the the "proxyReq" event is actually fired after the .web request.
Simply put console.log lines before and after proxy.web line and you will see what I am saying.
I am running into the same issue.
Same. Tried to write Set-Cookie header to res and it doesn't work
A workaround may be to delete the original header in proxyReq. Worked for an issue I had to rewrite content-type header.
A workaround may be to delete the original header in proxyReq.解决方法可能是删除 proxyReq 中的原始标头。 Worked for an issue I had to rewrite content-type header. 解决了一个问题,我必须重写内容类型标题。
thanks for this. i have same question in response header,worked for me to remove header in proxyRes.
configure: (proxy, options) => {
proxy.on('proxyRes', (proxyRes, req, res) => {
proxyRes.headers['content-type'] = undefined;
res.removeHeader('Content-Type');
res.setHeader('Content-Type', 'image/svg+xml');
proxyRes.pipe(res);
});
},