Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. as expected the proxy authorization did not work:

{ Host: 'api.clashofclans.com',
  'Proxy-Authorization': 'Basic token' }

Can anyone help me out?

like image 314
MarcoLe Avatar asked Jun 27 '18 17:06

MarcoLe


1 Answers

express-http-proxy allows you to pass in an options object (same object as used in the request library) via proxyReqOptDecorator:

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

like image 74
Chris Concannon Avatar answered Oct 02 '22 21:10

Chris Concannon