You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
*`error`: The error event is emitted if the request to the target fail.
201
-
*`proxyReq`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "web" connections
202
-
*`proxyReqWs`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "websocket" connections
203
-
*`proxyRes`: This event is emitted if the request to the target got a response.
204
-
*`open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
205
-
*`close`: This event is emitted once the proxy websocket was closed.
206
-
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
207
-
208
-
```js
209
-
var httpProxy =require('http-proxy');
210
-
// Error example
211
-
//
212
-
// Http Proxy Server with bad target
213
-
//
214
-
var proxy =httpProxy.createServer({
215
-
target:'http://localhost:9005'
216
-
});
217
-
218
-
proxy.listen(8005);
219
-
220
-
//
221
-
// Listen for the `error` event on `proxy`.
222
-
proxy.on('error', function (err, req, res) {
223
-
res.writeHead(500, {
224
-
'Content-Type':'text/plain'
225
-
});
226
-
227
-
res.end('Something went wrong. And we are reporting a custom error message.');
228
-
});
229
-
230
-
//
231
-
// Listen for the `proxyRes` event on `proxy`.
232
-
//
233
-
proxy.on('proxyRes', function (proxyRes, req, res) {
234
-
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
235
-
});
236
-
237
-
//
238
-
// Listen for the `open` event on `proxy`.
239
-
//
240
-
proxy.on('open', function (proxySocket) {
241
-
// listen for messages coming FROM the target here
242
-
proxySocket.on('data', hybiParseAndLogMessage);
243
-
});
244
-
245
-
//
246
-
// Listen for the `close` event on `proxy`.
247
-
//
248
-
proxy.on('close', function (req, socket, head) {
249
-
// view disconnected websocket connections
250
-
console.log('Client disconnected');
251
-
});
252
-
```
253
-
254
223
#### Using HTTPS
255
224
You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set `secure: true` in the options.
256
225
@@ -328,17 +297,6 @@ proxyServer.on('upgrade', function (req, socket, head) {
328
297
proxyServer.listen(8015);
329
298
```
330
299
331
-
#### ProxyTable API
332
-
A proxy table API is available through through this add-on [module](https://github.com/donasaur/http-proxy-rules), which lets you define a set of rules to translate matching routes to target routes that the reverse proxy will talk to.
333
-
334
-
### Contributing and Issues
335
-
336
-
* Search on Google/Github
337
-
* If you can't find anything, open an issue
338
-
* If you feel comfortable about fixing the issue, fork the repo
339
-
* Commit to your local branch (which must be different from `master`)
340
-
* Submit your Pull Request (be sure to include tests and update documentation)
341
-
342
300
### Options
343
301
344
302
`httpProxy.createProxyServer` supports the following options:
@@ -369,6 +327,62 @@ If you are using the `proxyServer.listen` method, the following options are also
369
327
***ssl**: object to be passed to https.createServer()
370
328
***ws**: true/false, if you want to proxy websockets
371
329
330
+
### Listening for proxy events
331
+
332
+
*`error`: The error event is emitted if the request to the target fail. **We do not do any error handling of messages passed between client and proxy, and messages passed between proxy and target, so it is recommended that you listen on errors and handle them.**
333
+
*`proxyReq`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "web" connections
334
+
*`proxyReqWs`: This event is emitted before the data is sent. It gives you a chance to alter the proxyReq request object. Applies to "websocket" connections
335
+
*`proxyRes`: This event is emitted if the request to the target got a response.
336
+
*`open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
337
+
*`close`: This event is emitted once the proxy websocket was closed.
338
+
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
339
+
340
+
```js
341
+
var httpProxy =require('http-proxy');
342
+
// Error example
343
+
//
344
+
// Http Proxy Server with bad target
345
+
//
346
+
var proxy =httpProxy.createServer({
347
+
target:'http://localhost:9005'
348
+
});
349
+
350
+
proxy.listen(8005);
351
+
352
+
//
353
+
// Listen for the `error` event on `proxy`.
354
+
proxy.on('error', function (err, req, res) {
355
+
res.writeHead(500, {
356
+
'Content-Type':'text/plain'
357
+
});
358
+
359
+
res.end('Something went wrong. And we are reporting a custom error message.');
360
+
});
361
+
362
+
//
363
+
// Listen for the `proxyRes` event on `proxy`.
364
+
//
365
+
proxy.on('proxyRes', function (proxyRes, req, res) {
366
+
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
367
+
});
368
+
369
+
//
370
+
// Listen for the `open` event on `proxy`.
371
+
//
372
+
proxy.on('open', function (proxySocket) {
373
+
// listen for messages coming FROM the target here
374
+
proxySocket.on('data', hybiParseAndLogMessage);
375
+
});
376
+
377
+
//
378
+
// Listen for the `close` event on `proxy`.
379
+
//
380
+
proxy.on('close', function (req, socket, head) {
381
+
// view disconnected websocket connections
382
+
console.log('Client disconnected');
383
+
});
384
+
```
385
+
372
386
### Shutdown
373
387
374
388
* When testing or running server within another program it may be necessary to close the proxy.
@@ -385,16 +399,30 @@ var proxy = new httpProxy.createProxyServer({
385
399
proxy.close();
386
400
```
387
401
388
-
### Test
402
+
### Miscellaneous
403
+
404
+
#### ProxyTable API
405
+
406
+
A proxy table API is available through through this add-on [module](https://github.com/donasaur/http-proxy-rules), which lets you define a set of rules to translate matching routes to target routes that the reverse proxy will talk to.
407
+
408
+
#### Test
389
409
390
410
```
391
411
$ npm test
392
412
```
393
413
394
-
### Logo
414
+
####Logo
395
415
396
416
Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
397
417
418
+
### Contributing and Issues
419
+
420
+
* Search on Google/Github
421
+
* If you can't find anything, open an issue
422
+
* If you feel comfortable about fixing the issue, fork the repo
423
+
* Commit to your local branch (which must be different from `master`)
424
+
* Submit your Pull Request (be sure to include tests and update documentation)
425
+
398
426
### License
399
427
400
428
>The MIT License (MIT)
@@ -418,5 +446,3 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
418
446
>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
419
447
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0 commit comments