villadora / express-http-proxy

Proxy middleware for express/connect
Other
1.22k stars 236 forks source link

proxyReqOptDecorator not setting headers as described #503

Closed seejwy closed 2 years ago

seejwy commented 2 years ago

The docs says that headers can be overridden before issuing the proxyRequest with the proxyReqOptDecorater with the following code.

proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
    // you can update headers
    proxyReqOpts.headers['Content-Type'] = 'text/html';
    // you can change the method
    proxyReqOpts.method = 'GET';
    return proxyReqOpts;
  }

but it does not seem to work as described.

let getProxy = proxy('someurl.com', {
    proxyReqOptDecorator: function (proxyReqOpts, srcReq) {
        srcReq.headers['X-Forwarded-For'] = 'test';
        return proxyReqOpts;
    }
});

Running express-http-proxy in debug mode, I observe that the 'proxy request options' and 'Request options (after processing)' both do not contain the header X-Forwarded-For.

If I were to pass the header from the client where X-Forwarded-For = not test, then the header appears in 'proxy request options'. However, after setting srcReq.headers['X-Forwarded-For'] = 'test'; the header remains unchanged in request options (after processing).

seejwy commented 2 years ago

I noticed I was using the wrong variable. Apologies. The issue would be that the network request header does not contain the appended headers.