[英]Node - express-http-proxy - set Header before proxying
Before I proxy to a address I want to set the header of the proxy (Smth like an interceptor). 在我代理到一个地址之前,我想设置代理的头(像拦截器一样)。 I use the express-http-library and express with Node.JS.
我使用express-http-library并通过Node.JS进行表达。 So far my code looks as follow.
到目前为止,我的代码如下。 Btw.
顺便说一句。 the docs of this library did not make me any wiser.
该库的文档并没有使我变得更明智。
app.use('/v1/*', proxy('velodrome.usefixie.com', {
userResHeaderDecorator(headers, userReq, userRes, proxyReq, proxyRes) {
// recieves an Object of headers, returns an Object of headers.
headers = {
Host: 'api.clashofclans.com',
'Proxy-Authorization': `Basic ${new Buffer('token').toString('base64')}`
};
console.log(headers);
return headers;
}
})); }));
And even though the console prints me out the headers obj. 即使控制台将我打印出标题obj。 as expected the proxy authorization did not work:
如预期的那样,代理授权不起作用:
{ Host: 'api.clashofclans.com',
'Proxy-Authorization': 'Basic token' }
Can anyone help me out? 谁能帮我吗?
If all you need to do is add some middleware to change some headers, you should be able to just do something like this: 如果您需要做的就是添加一些中间件来更改某些标头,那么您应该能够执行以下操作:
app.use('/v1/*', (req, res, next) => {
req.headers['Proxy-Authorization'] = `Basic ${new Buffer('token').toString('base64')}`;
next();
});
express-http-proxy
allows you to pass in an options object (same object as used in the request
library) via proxyReqOptDecorator
: express-http-proxy
允许您通过proxyReqOptDecorator
传递一个options对象(与request
库中使用的对象相同):
app.use("/proxy", proxy("https://target.io/api", {
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
proxyReqOpts.headers = {"Authorization": "Bearer token"};
return proxyReqOpts;
}
}));
or 要么
app.use("/proxy", proxy("https://target.io/api", {
proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
proxyReqOpts.auth = `${username}:${password}`;
return proxyReqOpts;
}
}));
The documentation for proxyReqOptDecorator
can be found here proxyReqOptDecorator
的文档可以在这里找到
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.