Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update response before sending #126

Closed
ArpanTanna opened this issue Jul 28, 2018 · 3 comments
Closed

Update response before sending #126

ArpanTanna opened this issue Jul 28, 2018 · 3 comments

Comments

@ArpanTanna
Copy link

I want to remove all image tags and other changes from response.
Where can I update response before sending?
My assumption is in cors-anywhere.js file, but can't figure out exact location.

@Rob--W
Copy link
Owner

Rob--W commented Jul 28, 2018

Transforming the response before sending is not supported.

If you want to, you can try it anyway as follows:

After this line,

return proxyReqOn.call(this, 'response', function(proxyRes) {

overwrite proxyRes.pipe with a custom function that intercepts the data, transforms it, and writes the result to res.

You should overwrite proxyRes.pipe because that will be called by the underlying library:
https://github.com/nodejitsu/node-http-proxy/blob/v1.11.1/lib/http-proxy/passes/web-incoming.js#L157

@Rob--W Rob--W closed this as completed Jul 28, 2018
@ArpanTanna
Copy link
Author

Thanks for quick reply!
But I haven't much information about pipe, better I can create one route and then call this function from that route.

Can you please let me know how can I add route?

@Rob--W
Copy link
Owner

Rob--W commented Jul 28, 2018

proxyRes is an instance of http.IncomingMessage, which is a readable stream (from where you can read the response to the proxied request).
res is an instance of http.ServerResponse, which is a writable stream (where you can write the response to the client).

When you replace proxyRes.pipe, you will be able to intercept responses on proxyRes via data events (and detect the end of stream via end events), which you can transform as desired, and when ready send the response to the client via res.write. See the linked documentation to learn more about interacting with readable and writable streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants