@@ -28,10 +28,14 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
2828
2929const app = express ();
3030
31- app .use (' /api' , createProxyMiddleware ({ target: ' http://www.example.org' , changeOrigin: true }));
31+ app .use (
32+ ' /api' ,
33+ createProxyMiddleware ({ target: ' http://www.example.org/secret' , changeOrigin: true })
34+ );
3235app .listen (3000 );
3336
34- // http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
37+ // proxy and change the base path from "/api" to "/secret"
38+ // http://localhost:3000/api/foo/bar -> http://www.example.org/secret/foo/bar
3539```
3640
3741``` typescript
@@ -42,9 +46,13 @@ import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-pro
4246
4347const app = express ();
4448
45- app .use (' /api' , createProxyMiddleware ({ target: ' http://www.example.org' , changeOrigin: true }));
49+ app .use (
50+ ' /api' ,
51+ createProxyMiddleware ({ target: ' http://www.example.org/api' , changeOrigin: true })
52+ );
4653app .listen (3000 );
4754
55+ // proxy and keep the same base path "/api"
4856// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
4957```
5058
@@ -57,7 +65,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
5765<!-- // spell-checker:disable -->
5866
5967- [ Install] ( #install )
60- - [ Core concept ] ( #core-concept )
68+ - [ Basic usage ] ( #basic-usage )
6169- [ Express Server Example] ( #express-server-example )
6270 - [ app.use(path, proxy)] ( #appusepath-proxy )
6371- [ Options] ( #options )
@@ -87,24 +95,23 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
8795npm install --save-dev http-proxy-middleware
8896```
8997
90- ## Core concept
98+ ## Basic usage
9199
92100Create and configure a proxy middleware with: ` createProxyMiddleware(config) ` .
93101
94102``` javascript
95103const { createProxyMiddleware } = require (' http-proxy-middleware' );
96104
97105const apiProxy = createProxyMiddleware ({
98- pathFilter: ' /api' ,
99106 target: ' http://www.example.org' ,
107+ changeOrigin: true ,
100108});
101109
102110// 'apiProxy' is now ready to be used as middleware in a server.
103111```
104112
105- - ** options.pathFilter** : Determine which requests should be proxied to the target host.
106- (more on [ path filter] ( #path-filter ) )
107113- ** options.target** : target host to proxy to. _ (protocol + host)_
114+ - ** options.changeOrigin** : for virtual hosted sites
108115
109116- see full list of [ ` http-proxy-middleware ` configuration options] ( #options )
110117
@@ -117,28 +124,19 @@ An example with `express` server.
117124const express = require (' express' );
118125const { createProxyMiddleware } = require (' http-proxy-middleware' );
119126
127+ const app = express ();
128+
120129// proxy middleware options
121130/** @type {import('http-proxy-middleware/dist/types').Options} */
122131const options = {
123- target: ' http://www.example.org' , // target host
132+ target: ' http://www.example.org/api ' , // target host with the same base path
124133 changeOrigin: true , // needed for virtual hosted sites
125- ws: true , // proxy websockets
126- pathRewrite: {
127- ' ^/api/old-path' : ' /api/new-path' , // rewrite path
128- ' ^/api/remove/path' : ' /path' , // remove base path
129- },
130- router: {
131- // when request.headers.host == 'dev.localhost:3000',
132- // override target 'http://www.example.org' to 'http://localhost:8000'
133- ' dev.localhost:3000' : ' http://localhost:8000' ,
134- },
135134};
136135
137136// create the proxy
138137const exampleProxy = createProxyMiddleware (options);
139138
140139// mount `exampleProxy` in web server
141- const app = express ();
142140app .use (' /api' , exampleProxy);
143141app .listen (3000 );
144142```
@@ -149,7 +147,13 @@ If you want to use the server's `app.use` `path` parameter to match requests.
149147Use ` pathFilter ` option to further include/exclude requests which you want to proxy.
150148
151149``` javascript
152- app .use (' /api' , createProxyMiddleware ({ target: ' http://www.example.org' , changeOrigin: true }));
150+ app .use (
151+ createProxyMiddleware ({
152+ target: ' http://www.example.org/api' ,
153+ changeOrigin: true ,
154+ pathFilter: ' /api/proxy-only-this-path' ,
155+ })
156+ );
153157```
154158
155159` app.use ` documentation:
@@ -164,20 +168,11 @@ http-proxy-middleware options:
164168
165169### ` pathFilter ` (string, [ ] string, glob, [ ] glob, function)
166170
167- 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.
168-
169- [ RFC 3986 ` path ` ] ( https://tools.ietf.org/html/rfc3986#section-3.3 ) is used in ` pathFilter ` .
170-
171- ``` ascii
172- foo://example.com:8042/over/there?name=ferret#nose
173- \_/ \______________/\_________/ \_________/ \__/
174- | | | | |
175- scheme authority path query fragment
176- ```
171+ Narrow down which requests should be proxied. The ` path ` used for filtering is the ` request.url ` pathname. In Express, this is the ` path ` relative to the mount-point of the proxy.
177172
178173- ** path matching**
179174
180- - ` createProxyMiddleware({...}) ` - matches any path, all requests will be proxied.
175+ - ` createProxyMiddleware({...}) ` - matches any path, all requests will be proxied when ` pathFilter ` is not configured .
181176 - ` createProxyMiddleware({ pathFilter: '/api', ...}) ` - matches paths starting with ` /api `
182177
183178- ** multiple path matching**
@@ -205,12 +200,13 @@ Decide which requests should be proxied; In case you are not able to use the ser
205200 /**
206201 * @return {Boolean}
207202 */
208- const filter = function (path , req ) {
203+ const pathFilter = function (path , req ) {
209204 return path .match (' ^/api' ) && req .method === ' GET' ;
210205 };
211206
212- const apiProxy = createProxyMiddleware (filter, {
207+ const apiProxy = createProxyMiddleware ({
213208 target: ' http://www.example.org' ,
209+ pathFilter: pathFilter,
214210 });
215211 ```
216212
0 commit comments