3
3
[ ![ Build Status] ( https://img.shields.io/travis/chimurai/http-proxy-middleware/master.svg?style=flat-square )] ( https://travis-ci.org/chimurai/http-proxy-middleware )
4
4
[ ![ Coveralls] ( https://img.shields.io/coveralls/chimurai/http-proxy-middleware.svg?style=flat-square )] ( https://coveralls.io/r/chimurai/http-proxy-middleware )
5
5
[ ![ dependency Status] ( https://img.shields.io/david/chimurai/http-proxy-middleware.svg?style=flat-square )] ( https://david-dm.org/chimurai/http-proxy-middleware#info=dependencies )
6
- [ ![ dependency Status] ( https://snyk.io/test/npm/http-proxy-middleware/badge.svg )] ( https://snyk.io/test/npm/http-proxy-middleware )
6
+ [ ![ dependency Status] ( https://snyk.io/test/npm/http-proxy-middleware/badge.svg?style=flat-square )] ( https://snyk.io/test/npm/http-proxy-middleware )
7
7
[ ![ code style: prettier] ( https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square )] ( https://github.com/prettier/prettier )
8
8
9
9
Node.js proxying made simple. Configure proxy middleware with ease for [ connect] ( https://github.com/senchalabs/connect ) , [ express] ( https://github.com/strongloop/express ) , [ browser-sync] ( https://github.com/BrowserSync/browser-sync ) and [ many more] ( #compatible-servers ) .
@@ -15,13 +15,16 @@ Powered by the popular Nodejitsu [`http-proxy`](https://github.com/nodejitsu/nod
15
15
Proxy ` /api ` requests to ` http://www.example.org `
16
16
17
17
``` javascript
18
- var express = require (' express' )
19
- var proxy = require (' http-proxy-middleware' )
18
+ var express = require (' express' );
19
+ var proxy = require (' http-proxy-middleware' );
20
20
21
- var app = express ()
21
+ var app = express ();
22
22
23
- app .use (' /api' , proxy ({ target: ' http://www.example.org' , changeOrigin: true }))
24
- app .listen (3000 )
23
+ app .use (
24
+ ' /api' ,
25
+ proxy ({ target: ' http://www.example.org' , changeOrigin: true })
26
+ );
27
+ app .listen (3000 );
25
28
26
29
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
27
30
```
@@ -68,9 +71,9 @@ Proxy middleware configuration.
68
71
#### proxy([ context,] config)
69
72
70
73
``` javascript
71
- var proxy = require (' http-proxy-middleware' )
74
+ var proxy = require (' http-proxy-middleware' );
72
75
73
- var apiProxy = proxy (' /api' , { target: ' http://www.example.org' })
76
+ var apiProxy = proxy (' /api' , { target: ' http://www.example.org' });
74
77
// \____/ \_____________________________/
75
78
// | |
76
79
// context options
@@ -88,7 +91,7 @@ var apiProxy = proxy('/api', { target: 'http://www.example.org' })
88
91
89
92
``` javascript
90
93
// shorthand syntax for the example above:
91
- var apiProxy = proxy (' http://www.example.org/api' )
94
+ var apiProxy = proxy (' http://www.example.org/api' );
92
95
```
93
96
94
97
More about the [ shorthand configuration] ( #shorthand ) .
@@ -99,8 +102,8 @@ An example with `express` server.
99
102
100
103
``` javascript
101
104
// include dependencies
102
- var express = require (' express' )
103
- var proxy = require (' http-proxy-middleware' )
105
+ var express = require (' express' );
106
+ var proxy = require (' http-proxy-middleware' );
104
107
105
108
// proxy middleware options
106
109
var options = {
@@ -116,15 +119,15 @@ var options = {
116
119
// override target 'http://www.example.org' to 'http://localhost:8000'
117
120
' dev.localhost:3000' : ' http://localhost:8000'
118
121
}
119
- }
122
+ };
120
123
121
124
// create the proxy (without context)
122
- var exampleProxy = proxy (options)
125
+ var exampleProxy = proxy (options);
123
126
124
127
// mount `exampleProxy` in web server
125
- var app = express ()
126
- app .use (' /api' , exampleProxy)
127
- app .listen (3000 )
128
+ var app = express ();
129
+ app .use (' /api' , exampleProxy);
130
+ app .listen (3000 );
128
131
```
129
132
130
133
## Context matching
@@ -172,10 +175,10 @@ Providing an alternative way to decide which requests should be proxied; In case
172
175
* @return {Boolean}
173
176
*/
174
177
var filter = function (pathname , req ) {
175
- return pathname .match (' ^/api' ) && req .method === ' GET'
176
- }
178
+ return pathname .match (' ^/api' ) && req .method === ' GET' ;
179
+ };
177
180
178
- var apiProxy = proxy (filter, { target: ' http://www.example.org' })
181
+ var apiProxy = proxy (filter, { target: ' http://www.example.org' });
179
182
```
180
183
181
184
## Options
@@ -224,23 +227,23 @@ Providing an alternative way to decide which requests should be proxied; In case
224
227
// simple replace
225
228
function logProvider (provider ) {
226
229
// replace the default console log provider.
227
- return require (' winston' )
230
+ return require (' winston' );
228
231
}
229
232
```
230
233
231
234
``` javascript
232
235
// verbose replacement
233
236
function logProvider (provider ) {
234
- var logger = new (require (' winston' )).Logger ()
237
+ var logger = new (require (' winston' )).Logger ();
235
238
236
239
var myCustomProvider = {
237
240
log: logger .log ,
238
241
debug: logger .debug ,
239
242
info: logger .info ,
240
243
warn: logger .warn ,
241
244
error: logger .error
242
- }
243
- return myCustomProvider
245
+ };
246
+ return myCustomProvider;
244
247
}
245
248
```
246
249
@@ -257,19 +260,19 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
257
260
function onError (err , req , res ) {
258
261
res .writeHead (500 , {
259
262
' Content-Type' : ' text/plain'
260
- })
263
+ });
261
264
res .end (
262
265
' Something went wrong. And we are reporting a custom error message.'
263
- )
266
+ );
264
267
}
265
268
```
266
269
267
270
- ** option.onProxyRes** : function, subscribe to http-proxy's ` proxyRes ` event.
268
271
269
272
``` javascript
270
273
function onProxyRes (proxyRes , req , res ) {
271
- proxyRes .headers [' x-added' ] = ' foobar' // add new header to response
272
- delete proxyRes .headers [' x-removed' ] // remove header from response
274
+ proxyRes .headers [' x-added' ] = ' foobar' ; // add new header to response
275
+ delete proxyRes .headers [' x-removed' ]; // remove header from response
273
276
}
274
277
```
275
278
@@ -278,7 +281,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
278
281
``` javascript
279
282
function onProxyReq (proxyReq , req , res ) {
280
283
// add custom header to request
281
- proxyReq .setHeader (' x-added' , ' foobar' )
284
+ proxyReq .setHeader (' x-added' , ' foobar' );
282
285
// or log the req
283
286
}
284
287
```
@@ -288,7 +291,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
288
291
``` javascript
289
292
function onProxyReqWs (proxyReq , req , socket , options , head ) {
290
293
// add custom header
291
- proxyReq .setHeader (' X-Special-Proxy-Header' , ' foobar' )
294
+ proxyReq .setHeader (' X-Special-Proxy-Header' , ' foobar' );
292
295
}
293
296
```
294
297
@@ -297,15 +300,15 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
297
300
``` javascript
298
301
function onOpen (proxySocket ) {
299
302
// listen for messages coming FROM the target here
300
- proxySocket .on (' data' , hybiParseAndLogMessage)
303
+ proxySocket .on (' data' , hybiParseAndLogMessage);
301
304
}
302
305
```
303
306
304
307
- ** option.onClose** : function, subscribe to http-proxy's ` close ` event.
305
308
``` javascript
306
309
function onClose (res , socket , head ) {
307
310
// view disconnected websocket connections
308
- console .log (' Client disconnected' )
311
+ console .log (' Client disconnected' );
309
312
}
310
313
```
311
314
@@ -383,13 +386,13 @@ The following options are provided by the underlying [http-proxy](https://github
383
386
Use the shorthand syntax when verbose configuration is not needed. The `context` and `option.target` will be automatically configured when shorthand is used. Options can still be used if needed.
384
387
385
388
```javascript
386
- proxy('http://www.example.org:8000/api')
389
+ proxy('http://www.example.org:8000/api');
387
390
// proxy('/api', {target: 'http://www.example.org:8000'});
388
391
389
- proxy('http://www.example.org:8000/api/books/*/**.json')
392
+ proxy('http://www.example.org:8000/api/books/*/**.json');
390
393
// proxy('/api/books/*/**.json', {target: 'http://www.example.org:8000'});
391
394
392
- proxy('http://www.example.org:8000/api', { changeOrigin: true })
395
+ proxy('http://www.example.org:8000/api', { changeOrigin: true });
393
396
// proxy('/api', {target: 'http://www.example.org:8000', changeOrigin: true});
394
397
```
395
398
@@ -399,7 +402,10 @@ If you want to use the server's `app.use` `path` parameter to match requests;
399
402
Create and mount the proxy without the http-proxy-middleware ` context ` parameter:
400
403
401
404
``` javascript
402
- app .use (' /api' , proxy ({ target: ' http://www.example.org' , changeOrigin: true }))
405
+ app .use (
406
+ ' /api' ,
407
+ proxy ({ target: ' http://www.example.org' , changeOrigin: true })
408
+ );
403
409
```
404
410
405
411
` app.use ` documentation:
@@ -411,27 +417,27 @@ app.use('/api', proxy({ target: 'http://www.example.org', changeOrigin: true }))
411
417
412
418
``` javascript
413
419
// verbose api
414
- proxy (' /' , { target: ' http://echo.websocket.org' , ws: true })
420
+ proxy (' /' , { target: ' http://echo.websocket.org' , ws: true });
415
421
416
422
// shorthand
417
- proxy (' http://echo.websocket.org' , { ws: true })
423
+ proxy (' http://echo.websocket.org' , { ws: true });
418
424
419
425
// shorter shorthand
420
- proxy (' ws://echo.websocket.org' )
426
+ proxy (' ws://echo.websocket.org' );
421
427
```
422
428
423
429
### External WebSocket upgrade
424
430
425
431
In the previous WebSocket examples, http-proxy-middleware relies on a initial http request in order to listen to the http ` upgrade ` event. If you need to proxy WebSockets without the initial http request, you can subscribe to the server's http ` upgrade ` event manually.
426
432
427
433
``` javascript
428
- var wsProxy = proxy (' ws://echo.websocket.org' , { changeOrigin: true })
434
+ var wsProxy = proxy (' ws://echo.websocket.org' , { changeOrigin: true });
429
435
430
- var app = express ()
431
- app .use (wsProxy)
436
+ var app = express ();
437
+ app .use (wsProxy);
432
438
433
- var server = app .listen (3000 )
434
- server .on (' upgrade' , wsProxy .upgrade ) // <-- subscribe to http 'upgrade'
439
+ var server = app .listen (3000 );
440
+ server .on (' upgrade' , wsProxy .upgrade ); // <-- subscribe to http 'upgrade'
435
441
```
436
442
437
443
## Working examples
@@ -468,16 +474,20 @@ Run the test suite:
468
474
469
475
``` bash
470
476
# install dependencies
471
- $ npm install
477
+ $ yarn
472
478
473
479
# linting
474
- $ npm run lint
480
+ $ yarn lint
481
+ $ yarn lint:fix
482
+
483
+ # building (compile typescript to js)
484
+ $ yarn build
475
485
476
486
# unit tests
477
- $ npm test
487
+ $ yarn test
478
488
479
489
# code coverage
480
- $ npm run cover
490
+ $ yarn cover
481
491
```
482
492
483
493
## Changelog
@@ -488,4 +498,4 @@ $ npm run cover
488
498
489
499
The MIT License (MIT)
490
500
491
- Copyright (c) 2015-2018 Steven Chim
501
+ Copyright (c) 2015-2019 Steven Chim
0 commit comments