Skip to content

Commit 4f2bc58

Browse files
committed
Add documentation for listening for proxy events to prevent a common mistake.
1 parent 99ee542 commit 4f2bc58

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,32 @@ proxyServerWithForwarding.listen(80);
217217

218218
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'.
219219

220+
### Listening for proxy events
221+
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.
222+
223+
``` js
224+
var httpProxy = require('http-proxy');
225+
226+
var server = httpProxy.createServer(function (req, res, proxy) {
227+
228+
var buffer = httpProxy.buffer(req);
229+
230+
proxy.proxyRequest(req, res, {
231+
host: '127.0.0.1',
232+
port: 9000,
233+
buffer: buffer
234+
});
235+
236+
});
237+
238+
server.proxy.on('end', function() {
239+
console.log("The request was proxied.");
240+
});
241+
242+
server.listen(8000);
243+
```
244+
245+
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.
220246

221247
## Using HTTPS
222248
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.

0 commit comments

Comments
 (0)