-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: master
Are you sure you want to change the base?
Conversation
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();
}
}); |
+1 we needed this in order to transparently follow 307 redirects in the proxy for the client |
@indexzero, @jcrugzz: I can rebase this on the latest master if you guys are open to merging it. |
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. |
@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 |
This is super useful. Sad to see it wasn't merged |
👍 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. |
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. |
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.