Rob--W / cors-anywhere

CORS Anywhere is a NodeJS reverse proxy which adds CORS headers to the proxied request.
MIT License
8.57k stars 5.99k forks source link

Modify the response body #429

Closed asheroto closed 2 years ago

asheroto commented 2 years ago

Hi there,

Thanks for the great tool! I'm searching for a way to modify the response body of a proxied request. I understand you can specify httpProxyOptions.

Where exactly should I specify/place the code mentioned for cors-anywhere using the modify body technique? I tried doing...

    var option = {
      target: target,
      selfHandleResponse : true
    };
    cors_proxy.on('proxyRes', function (proxyRes, req, res) {
        var body = [];
        proxyRes.on('data', function (chunk) {
            body.push(chunk);
        });
        proxyRes.on('end', function () {
            body = Buffer.concat(body).toString();
            res.end(body.replace("https://","https://URLHERE/https://");
        });
    });
    cors_proxy.web(req, res, option);

after }).listen(port, host, function but I'm not strong enough with JS to understand what to do here. Thanks for your help and patience with this.

Or can I somehow modify cors_proxy.createServer to use the traditional function method such as...

cors_proxy.createServer((req, res) => {

And modify the body via response there?

I saw [https://github.com/http-party/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js](this example) but am unsure how to utilize it and cors_proxy.

Thank you, I am grateful for your time. :-)

Rob--W commented 2 years ago

Modification of response bodies is far beyond the scope of this library and not supported.

cors-anywhere currently uses a specific version of http-proxy, where the selfHandleResponse option is not supported. If you'd like to use a library to help with proxying, it would be easier to use the underlying http-proxy library directly (i.e. https://github.com/http-party/node-http-proxy) and add CORS headers when needed, rather than trying to build on top of this one.

asheroto commented 2 years ago

Thank you.