Merge pull request #216 from CodeRarity/master

Re-emit 'start', 'forward' and 'end' events in RoutingProxy, and fix some hanging issues.
This commit is contained in:
Christian Howe 2012-05-10 09:34:46 -07:00
commit f223ce8b4e
2 changed files with 73 additions and 29 deletions

View File

@ -217,6 +217,32 @@ proxyServerWithForwarding.listen(80);
The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'. The forwarding option can be used in conjunction with the proxy table options by simply including both the 'forward' and 'router' properties in the options passed to 'createServer'.
### Listening for proxy events
Sometimes you want to listen to an event on a proxy. For example, you may want to listen to the 'end' event, which represents when the proxy has finished proxying a request.
``` js
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: 9000,
buffer: buffer
});
});
server.proxy.on('end', function() {
console.log("The request was proxied.");
});
server.listen(8000);
```
It's important to remember not to listen for events on the proxy object in the function passed to `httpProxy.createServer`. Doing so would add a new listener on every request, which would end up being a disaster.
## Using HTTPS ## Using HTTPS
You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server. You have all the full flexibility of node-http-proxy offers in HTTPS as well as HTTP. The two basic scenarios are: with a stand-alone proxy server or in conjunction with another HTTPS server.

View File

@ -51,6 +51,18 @@ var RoutingProxy = exports.RoutingProxy = function (options) {
this.https = this.source.https || options.https; this.https = this.source.https || options.https;
this.enable = options.enable; this.enable = options.enable;
this.forward = options.forward; this.forward = options.forward;
//
// Listen for 'newListener' events so that we can bind 'proxyError'
// listeners to each HttpProxy's 'proxyError' event.
//
this.on('newListener', function (evt) {
if (evt === 'proxyError' || evt === 'webSocketProxyError') {
Object.keys(self.proxies).forEach(function (key) {
self.proxies[key].on(evt, this.emit.bind(this, evt));
});
}
});
}; };
@ -90,8 +102,15 @@ RoutingProxy.prototype.add = function (options) {
}); });
this.proxies[key] = new HttpProxy(options); this.proxies[key] = new HttpProxy(options);
this.proxies[key].on('proxyError', this.emit.bind(this, 'proxyError')); if (this.listeners('proxyError').length > 0) {
this.proxies[key].on('webSocketProxyError', this.emit.bind(this, 'webSocketProxyError')); this.proxies[key].on('proxyError', this.emit.bind(this, 'proxyError'));
}
if (this.listeners('webSocketProxyError').length > 0) {
this.proxies[key].on('webSocketProxyError', this.emit.bind(this, 'webSocketProxyError'));
}
this.proxies[key].on('start', this.emit.bind(this, 'start'));
this.proxies[key].on('forward', this.emit.bind(this, 'forward'));
this.proxies[key].on('end', this.emit.bind(this, 'end'));
}; };
// //
@ -185,7 +204,6 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
if (!this.proxies[key]) { if (!this.proxies[key]) {
this.add(options); this.add(options);
} }
proxy = this.proxies[key]; proxy = this.proxies[key];