Skip to content

Manipulate or cancel proxyRes #850

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

cmawhorter
Copy link

It'd be nice to have the ability to manipulate or abort a proxyRes. This is a solution for #737.

Not the prettiest but is 100% backwards compat and has almost non-existent perf penalty. Test included.

@cmawhorter
Copy link
Author

Guess I should include an example:

proxy.on('proxyRes', function(proxyRes, req, res) {
  // conditionally proxy to a different target 
  if (proxyRes.statusCode === 404) {
    proxyRes.abort(); // leave http-proxy handling
    proxy.web(req, res, { target: 'http://a-different.example.com' });
  }
  // respond directly
  else if (proxyRes.statusCode === 301) {
    proxyRes.abort();
    var someStream = ...;
    someStream.pipe(res);
    res.end();
  }
});

@prust
Copy link

prust commented Jan 20, 2016

+1 we needed this in order to transparently follow 307 redirects in the proxy for the client

@prust
Copy link

prust commented Jan 20, 2016

@indexzero, @jcrugzz: I can rebase this on the latest master if you guys are open to merging it.

@euprogramador
Copy link

Guys,

I think exactly like that, but I would like to have control over the continuation of the flow as between sending the response from the server to the client I want to run some asynchronous code.

@acanimal
Copy link

@euprogramador I don't know if it is exactly what you need, but some time ago I was working on ClydeIO project (now stopped due I have no free time to spent on it) which allows to configure a set of middlewares before/after sending request to http-proxy. See image: https://github.com/clydeio/clydeio/wiki/Data-Workflow

@thetrevdev
Copy link

This is super useful. Sad to see it wasn't merged

@sibnerian
Copy link

sibnerian commented Mar 4, 2018

👍 Among other things, this is extremely useful for catching a proxy error page and sending a prettier version. Would love to see this merged!

For posterity: I was able to hack around it by overriding the ServerResponse methods after sending the response.

onProxyRes(proxyRes, req, res) {
  if (proxyRes.statusCode >= 400) {
    renderErrorResponse(req, res);
    // Overwrite the methods in https://nodejs.org/api/http.html#http_class_http_serverresponse
    ['addTrailers', 'end', 'setHeader', 'write', 'writeContinue', 'writeHead'].forEach((k) => {
      res[k] = noop;
    });
  }
}

This is a hack, so caveat emptor. It won't work if renderErrorResponse is asynchronous. I'll be getting rid of this when (if?) this PR is merged.

@cmawhorter
Copy link
Author

cmawhorter commented Apr 9, 2018

anyone still in need of a nodejs proxy should look at anyproxy. it has a ton of its own problems, but is at least actively maintained by alibaba. it's perfect for dev, which is all i ever used node proxy for anyway.

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

Successfully merging this pull request may close these issues.

6 participants