@@ -56,13 +56,16 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
56
56
57
57
- [ Install] ( #install )
58
58
- [ Core concept] ( #core-concept )
59
- - [ Example] ( #example )
59
+ - [ Express Server Example] ( #express-server- example )
60
60
- [ app.use(path, proxy)] ( #appusepath-proxy )
61
- - [ Context matching] ( #context-matching )
62
61
- [ Options] ( #options )
63
- - [ http-proxy-middleware options] ( #http-proxy-middleware-options )
64
- - [ http-proxy events] ( #http-proxy-events )
65
- - [ http-proxy options] ( #http-proxy-options )
62
+ - [ ` pathFilter ` (string, [ ] string, glob, [ ] glob, function)] ( #pathfilter-string-string-glob-glob-function )
63
+ - [ ` pathRewrite ` (object/function)] ( #pathrewrite-objectfunction )
64
+ - [ ` router ` (object/function)] ( #router-objectfunction )
65
+ - [ ` logLevel ` (string)] ( #loglevel-string )
66
+ - [ ` logProvider ` (function)] ( #logprovider-function )
67
+ - [ ` http-proxy ` events] ( #http-proxy-events )
68
+ - [ ` http-proxy ` options] ( #http-proxy-options )
66
69
- [ WebSocket] ( #websocket )
67
70
- [ External WebSocket upgrade] ( #external-websocket-upgrade )
68
71
- [ Intercept and manipulate requests] ( #intercept-and-manipulate-requests )
@@ -76,34 +79,32 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
76
79
77
80
## Install
78
81
79
- ``` bash
80
- $ npm install --save-dev http-proxy-middleware
82
+ ``` shell
83
+ npm install --save-dev http-proxy-middleware
81
84
```
82
85
83
86
## Core concept
84
87
85
- Proxy middleware configuration.
86
-
87
- #### createProxyMiddleware([ context,] config)
88
+ Create and configure a proxy middleware with: ` createProxyMiddleware(config) ` .
88
89
89
90
``` javascript
90
91
const { createProxyMiddleware } = require (' http-proxy-middleware' );
91
92
92
- const apiProxy = createProxyMiddleware (' /api ' , { target : ' http://www.example.org ' });
93
- // \____/ \_____________________________/
94
- // | |
95
- // context options
93
+ const apiProxy = createProxyMiddleware ({
94
+ pathFilter : ' /api ' ,
95
+ target : ' http://www.example.org ' ,
96
+ });
96
97
97
98
// 'apiProxy' is now ready to be used as middleware in a server.
98
99
```
99
100
100
- - ** context ** : Determine which requests should be proxied to the target host.
101
- (more on [ context matching ] ( #context-matching ) )
101
+ - ** options.pathFilter ** : Determine which requests should be proxied to the target host.
102
+ (more on [ path filter ] ( #path-filter ) )
102
103
- ** options.target** : target host to proxy to. _ (protocol + host)_
103
104
104
- ( full list of [ ` http-proxy-middleware ` configuration options] ( #options ) )
105
+ - see full list of [ ` http-proxy-middleware ` configuration options] ( #options )
105
106
106
- ## Example
107
+ ## Express Server Example
107
108
108
109
An example with ` express ` server.
109
110
@@ -129,7 +130,7 @@ const options = {
129
130
},
130
131
};
131
132
132
- // create the proxy (without context)
133
+ // create the proxy
133
134
const exampleProxy = createProxyMiddleware (options);
134
135
135
136
// mount `exampleProxy` in web server
@@ -140,8 +141,8 @@ app.listen(3000);
140
141
141
142
### app.use(path, proxy)
142
143
143
- If you want to use the server's ` app.use ` ` path ` parameter to match requests;
144
- Create and mount the proxy without the http-proxy-middleware ` context ` parameter:
144
+ If you want to use the server's ` app.use ` ` path ` parameter to match requests.
145
+ Use ` pathFilter ` option to further include/exclude requests which you want to proxy.
145
146
146
147
``` javascript
147
148
app .use (' /api' , createProxyMiddleware ({ target: ' http://www.example.org' , changeOrigin: true }));
@@ -153,11 +154,15 @@ app.use('/api', createProxyMiddleware({ target: 'http://www.example.org', change
153
154
- connect: https://github.com/senchalabs/connect#mount-middleware
154
155
- polka: https://github.com/lukeed/polka#usebase-fn
155
156
156
- ## Context matching
157
+ ## Options
158
+
159
+ http-proxy-middleware options:
157
160
158
- Providing an alternative way to decide which requests should be proxied; In case you are not able to use the server's [ ` path ` parameter ] ( http://expressjs.com/en/4x/api.html#app.use ) to mount the proxy or when you need more flexibility.
161
+ ### ` pathFilter ` (string, [ ] string, glob, [ ] glob, function)
159
162
160
- [ RFC 3986 ` path ` ] ( https://tools.ietf.org/html/rfc3986#section-3.3 ) is used for context matching.
163
+ Decide which requests should be proxied; In case you are not able to use the server's [ ` path ` parameter] ( http://expressjs.com/en/4x/api.html#app.use ) to mount the proxy or when you need more flexibility.
164
+
165
+ [ RFC 3986 ` path ` ] ( https://tools.ietf.org/html/rfc3986#section-3.3 ) is used in ` pathFilter ` .
161
166
162
167
``` ascii
163
168
foo://example.com:8042/over/there?name=ferret#nose
@@ -169,23 +174,22 @@ Providing an alternative way to decide which requests should be proxied; In case
169
174
- ** path matching**
170
175
171
176
- ` createProxyMiddleware({...}) ` - matches any path, all requests will be proxied.
172
- - ` createProxyMiddleware('/', {...}) ` - matches any path, all requests will be proxied.
173
- - ` createProxyMiddleware('/api', {...}) ` - matches paths starting with ` /api `
177
+ - ` createProxyMiddleware({ pathFilter: '/api', ...}) ` - matches paths starting with ` /api `
174
178
175
179
- ** multiple path matching**
176
180
177
- - ` createProxyMiddleware(['/api', '/ajax', '/someotherpath'], { ...}) `
181
+ - ` createProxyMiddleware({ pathFilter: ['/api', '/ajax', '/someotherpath'], ...}) `
178
182
179
183
- ** wildcard path matching**
180
184
181
185
For fine-grained control you can use wildcard matching. Glob pattern matching is done by _ micromatch_ . Visit [ micromatch] ( https://www.npmjs.com/package/micromatch ) or [ glob] ( https://www.npmjs.com/package/glob ) for more globbing examples.
182
186
183
- - ` createProxyMiddleware('**', { ...}) ` matches any path, all requests will be proxied.
184
- - ` createProxyMiddleware('**/*.html', { ...}) ` matches any path which ends with ` .html `
185
- - ` createProxyMiddleware('/*.html', { ...}) ` matches paths directly under path-absolute
186
- - ` createProxyMiddleware('/api/**/*.html', { ...}) ` matches requests ending with ` .html ` in the path of ` /api `
187
- - ` createProxyMiddleware(['/api/**', '/ajax/**'], { ...}) ` combine multiple patterns
188
- - ` createProxyMiddleware(['/api/**', '!**/bad.json'], { ...}) ` exclusion
187
+ - ` createProxyMiddleware({ pathFilter: '**', ...}) ` matches any path, all requests will be proxied.
188
+ - ` createProxyMiddleware({ pathFilter: '**/*.html', ...}) ` matches any path which ends with ` .html `
189
+ - ` createProxyMiddleware({ pathFilter: '/*.html', ...}) ` matches paths directly under path-absolute
190
+ - ` createProxyMiddleware({ pathFilter: '/api/**/*.html', ...}) ` matches requests ending with ` .html ` in the path of ` /api `
191
+ - ` createProxyMiddleware({ pathFilter: ['/api/**', '/ajax/**'], ...}) ` combine multiple patterns
192
+ - ` createProxyMiddleware({ pathFilter: ['/api/**', '!**/bad.json'], ...}) ` exclusion
189
193
190
194
** Note** : In multiple path matching, you cannot use string paths and wildcard paths together.
191
195
@@ -197,104 +201,110 @@ Providing an alternative way to decide which requests should be proxied; In case
197
201
/**
198
202
* @return {Boolean}
199
203
*/
200
- const filter = function (pathname , req ) {
201
- return pathname .match (' ^/api' ) && req .method === ' GET' ;
204
+ const filter = function (path , req ) {
205
+ return path .match (' ^/api' ) && req .method === ' GET' ;
202
206
};
203
207
204
208
const apiProxy = createProxyMiddleware (filter, {
205
209
target: ' http://www.example.org' ,
206
210
});
207
211
```
208
212
209
- ## Options
213
+ ### ` pathRewrite ` (object/function)
210
214
211
- ### http-proxy-middleware options
215
+ Rewrite target's url path. Object-keys will be used as _ RegExp _ to match paths.
212
216
213
- - ** option.pathRewrite** : object/function, rewrite target's url path. Object-keys will be used as _ RegExp_ to match paths.
217
+ ``` javascript
218
+ // rewrite path
219
+ pathRewrite: {' ^/old/api' : ' /new/api' }
214
220
215
- ``` javascript
216
- // rewrite path
217
- pathRewrite: {' ^/old/api' : ' /new/api' }
221
+ // remove path
222
+ pathRewrite: {' ^/remove/api' : ' ' }
218
223
219
- // remove path
220
- pathRewrite: {' ^/remove/api ' : ' ' }
224
+ // add base path
225
+ pathRewrite: {' ^/' : ' /basepath/ ' }
221
226
222
- // add base path
223
- pathRewrite: { ' ^/ ' : ' /basepath/ ' }
227
+ // custom rewriting
228
+ pathRewrite : function ( path , req ) { return path . replace ( ' /api ' , ' /base/api ' ) }
224
229
225
- // custom rewriting
226
- pathRewrite : function (path , req ) { return path .replace (' /api' , ' /base/api' ) }
230
+ // custom rewriting, returning Promise
231
+ pathRewrite : async function (path , req ) {
232
+ const should_add_something = await httpRequestToDecideSomething (path);
233
+ if (should_add_something) path += " something" ;
234
+ return path;
235
+ }
236
+ ```
227
237
228
- // custom rewriting, returning Promise
229
- pathRewrite : async function (path , req ) {
230
- const should_add_something = await httpRequestToDecideSomething (path);
231
- if (should_add_something) path += " something" ;
232
- return path;
233
- }
234
- ```
238
+ ### ` router ` (object/function)
235
239
236
- - ** option.router ** : object/function, re -target ` option.target ` for specific requests.
240
+ Re -target ` option.target ` for specific requests.
237
241
238
- ``` javascript
239
- // Use `host` and/or `path` to match requests. First match will be used.
240
- // The order of the configuration matters.
241
- router: {
242
- ' integration.localhost:3000' : ' http://localhost:8001' , // host only
243
- ' staging.localhost:3000' : ' http://localhost:8002' , // host only
244
- ' localhost:3000/api' : ' http://localhost:8003' , // host + path
245
- ' /rest' : ' http://localhost:8004' // path only
246
- }
242
+ ``` javascript
243
+ // Use `host` and/or `path` to match requests. First match will be used.
244
+ // The order of the configuration matters.
245
+ router: {
246
+ ' integration.localhost:3000' : ' http://localhost:8001' , // host only
247
+ ' staging.localhost:3000' : ' http://localhost:8002' , // host only
248
+ ' localhost:3000/api' : ' http://localhost:8003' , // host + path
249
+ ' /rest' : ' http://localhost:8004' // path only
250
+ }
251
+
252
+ // Custom router function (string target)
253
+ router : function (req ) {
254
+ return ' http://localhost:8004' ;
255
+ }
256
+
257
+ // Custom router function (target object)
258
+ router : function (req ) {
259
+ return {
260
+ protocol: ' https:' , // The : is required
261
+ host: ' localhost' ,
262
+ port: 8004
263
+ };
264
+ }
247
265
248
- // Custom router function (string target)
249
- router : function (req ) {
250
- return ' http://localhost:8004' ;
251
- }
266
+ // Asynchronous router function which returns promise
267
+ router : async function (req ) {
268
+ const url = await doSomeIO ();
269
+ return url;
270
+ }
271
+ ```
252
272
253
- // Custom router function (target object)
254
- router : function (req ) {
255
- return {
256
- protocol: ' https:' , // The : is required
257
- host: ' localhost' ,
258
- port: 8004
259
- };
260
- }
273
+ ### ` logLevel ` (string)
261
274
262
- // Asynchronous router function which returns promise
263
- router : async function (req ) {
264
- const url = await doSomeIO ();
265
- return url;
266
- }
267
- ```
275
+ Default: ` 'info' `
268
276
269
- - ** option.logLevel ** : string, [ 'debug', 'info', 'warn', 'error', 'silent'] . Default: ` 'info' `
277
+ Values: [ 'debug', 'info', 'warn', 'error', 'silent'] .
270
278
271
- - ** option. logProvider** : function, modify or replace log provider. Default: ` console ` .
279
+ ### ` logProvider ` ( function)
272
280
273
- ``` javascript
274
- // simple replace
275
- function logProvider (provider ) {
276
- // replace the default console log provider.
277
- return require (' winston' );
278
- }
279
- ```
281
+ Modify or replace log provider. Default: ` console ` .
280
282
281
- ``` javascript
282
- // verbose replacement
283
- function logProvider (provider ) {
284
- const logger = new (require (' winston' ).Logger )();
285
-
286
- const myCustomProvider = {
287
- log: logger .log ,
288
- debug: logger .debug ,
289
- info: logger .info ,
290
- warn: logger .warn ,
291
- error: logger .error ,
292
- };
293
- return myCustomProvider;
294
- }
295
- ```
283
+ ``` javascript
284
+ // simple replace
285
+ function logProvider (provider ) {
286
+ // replace the default console log provider.
287
+ return require (' winston' );
288
+ }
289
+ ```
290
+
291
+ ``` javascript
292
+ // verbose replacement
293
+ function logProvider (provider ) {
294
+ const logger = new (require (' winston' ).Logger )();
295
+
296
+ const myCustomProvider = {
297
+ log: logger .log ,
298
+ debug: logger .debug ,
299
+ info: logger .info ,
300
+ warn: logger .warn ,
301
+ error: logger .error ,
302
+ };
303
+ return myCustomProvider;
304
+ }
305
+ ```
296
306
297
- ### http-proxy events
307
+ ## ` http-proxy ` events
298
308
299
309
Subscribe to [ http-proxy events] ( https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events ) :
300
310
@@ -355,7 +365,7 @@ Subscribe to [http-proxy events](https://github.com/nodejitsu/node-http-proxy#li
355
365
}
356
366
```
357
367
358
- ### http-proxy options
368
+ ## ` http-proxy ` options
359
369
360
370
The following options are provided by the underlying [ http-proxy] ( https://github.com/nodejitsu/node-http-proxy#options ) library.
361
371
@@ -431,15 +441,15 @@ The following options are provided by the underlying [http-proxy](https://github
431
441
432
442
``` javascript
433
443
// verbose api
434
- createProxyMiddleware (' /' , { target: ' http://echo.websocket.org' , ws: true });
444
+ createProxyMiddleware ({ pathFilter : ' /' , target: ' http://echo.websocket.org' , ws: true });
435
445
```
436
446
437
447
### External WebSocket upgrade
438
448
439
449
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.
440
450
441
451
``` javascript
442
- const wsProxy = createProxyMiddleware (' ws://echo.websocket.org' , { changeOrigin: true });
452
+ const wsProxy = createProxyMiddleware ({ target : ' ws://echo.websocket.org' , changeOrigin: true });
443
453
444
454
const app = express ();
445
455
app .use (wsProxy);
0 commit comments