-
Notifications
You must be signed in to change notification settings - Fork 2k
Re-emit 'start', 'forward' and 'end' events in RoutingProxy #216
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
Conversation
The reason that this case exists is because you only have access to the RoutingProxy inside of the server function. The RoutingProxy never gets recreated between requests, so it will end up adding a new listener every time a request is made. One solution to get around this right now is to not use var http = require('http'),
httpProxy = require('http-proxy');
var proxy = new httpProxy.RoutingProxy();
proxy.on('end', function() {
console.log('proxied');
})
http.createServer(function (req, res) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 8080
});
}).listen(8000); You could also use the var httpProxy = require('http-proxy');
var server = httpProxy.createServer(function (req, res, proxy) {
var buffer = httpProxy.buffer(req);
proxy.proxyRequest(req, res, {
host: '127.0.0.1',
port: 8080,
buffer: buffer
});
});
server.proxy.on('end', function() {
console.log("The request was proxied (server)");
});
server.listen(8000); I'm not sure how to fix the case with listening for events on the proxy object inside the server function. You could remove the old listener every request, but adding and removing a listener on every request is very inefficient. If it recreated the |
+1 |
This last commit fixes many problems people have had with |
@coderarity Looks good to me. Merge at will. |
Re-emit 'start', 'forward' and 'end' events in RoutingProxy, and fix some hanging issues.
Fixes #214. This will re-emit more events from RoutingProxy so that you can listen for those events yourself. Be VERY careful for the following case, which will add a new listener for every request:
It would be good if we could fix this case! This is a very easy problem to run into!