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
-
```
237
+
**[Back to top](#table-of-contents)**
253
238
254
239
#### Using HTTPS
255
240
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.
@@ -288,6 +273,8 @@ httpProxy.createServer({
288
273
}).listen(443);
289
274
```
290
275
276
+
**[Back to top](#table-of-contents)**
277
+
291
278
#### Proxying WebSockets
292
279
You can activate the websocket support for the proxy using `ws:true` in the options.
293
280
@@ -328,16 +315,7 @@ proxyServer.on('upgrade', function (req, socket, head) {
328
315
proxyServer.listen(8015);
329
316
```
330
317
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)
318
+
**[Back to top](#table-of-contents)**
341
319
342
320
### Options
343
321
@@ -369,6 +347,66 @@ If you are using the `proxyServer.listen` method, the following options are also
369
347
***ssl**: object to be passed to https.createServer()
370
348
***ws**: true/false, if you want to proxy websockets
371
349
350
+
**[Back to top](#table-of-contents)**
351
+
352
+
### Listening for proxy events
353
+
354
+
*`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.**
355
+
*`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
356
+
*`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
357
+
*`proxyRes`: This event is emitted if the request to the target got a response.
358
+
*`open`: This event is emitted once the proxy websocket was created and piped into the target websocket.
359
+
*`close`: This event is emitted once the proxy websocket was closed.
360
+
* (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
361
+
362
+
```js
363
+
var httpProxy =require('http-proxy');
364
+
// Error example
365
+
//
366
+
// Http Proxy Server with bad target
367
+
//
368
+
var proxy =httpProxy.createServer({
369
+
target:'http://localhost:9005'
370
+
});
371
+
372
+
proxy.listen(8005);
373
+
374
+
//
375
+
// Listen for the `error` event on `proxy`.
376
+
proxy.on('error', function (err, req, res) {
377
+
res.writeHead(500, {
378
+
'Content-Type':'text/plain'
379
+
});
380
+
381
+
res.end('Something went wrong. And we are reporting a custom error message.');
382
+
});
383
+
384
+
//
385
+
// Listen for the `proxyRes` event on `proxy`.
386
+
//
387
+
proxy.on('proxyRes', function (proxyRes, req, res) {
388
+
console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2));
389
+
});
390
+
391
+
//
392
+
// Listen for the `open` event on `proxy`.
393
+
//
394
+
proxy.on('open', function (proxySocket) {
395
+
// listen for messages coming FROM the target here
396
+
proxySocket.on('data', hybiParseAndLogMessage);
397
+
});
398
+
399
+
//
400
+
// Listen for the `close` event on `proxy`.
401
+
//
402
+
proxy.on('close', function (req, socket, head) {
403
+
// view disconnected websocket connections
404
+
console.log('Client disconnected');
405
+
});
406
+
```
407
+
408
+
**[Back to top](#table-of-contents)**
409
+
372
410
### Shutdown
373
411
374
412
* When testing or running server within another program it may be necessary to close the proxy.
@@ -385,16 +423,36 @@ var proxy = new httpProxy.createProxyServer({
385
423
proxy.close();
386
424
```
387
425
388
-
### Test
426
+
**[Back to top](#table-of-contents)**
427
+
428
+
### Miscellaneous
429
+
430
+
#### ProxyTable API
431
+
432
+
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.
433
+
434
+
#### Test
389
435
390
436
```
391
437
$ npm test
392
438
```
393
439
394
-
### Logo
440
+
####Logo
395
441
396
442
Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
397
443
444
+
**[Back to top](#table-of-contents)**
445
+
446
+
### Contributing and Issues
447
+
448
+
* Search on Google/Github
449
+
* If you can't find anything, open an issue
450
+
* If you feel comfortable about fixing the issue, fork the repo
451
+
* Commit to your local branch (which must be different from `master`)
452
+
* Submit your Pull Request (be sure to include tests and update documentation)
453
+
454
+
**[Back to top](#table-of-contents)**
455
+
398
456
### License
399
457
400
458
>The MIT License (MIT)
@@ -418,5 +476,3 @@ Logo created by [Diego Pasquali](http://dribbble.com/diegopq)
418
476
>LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
419
477
>OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0 commit comments