@@ -28,10 +28,14 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
28
28
29
29
const app = express ();
30
30
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
+ );
32
35
app .listen (3000 );
33
36
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
35
39
```
36
40
37
41
``` typescript
@@ -42,9 +46,13 @@ import { createProxyMiddleware, Filter, Options, RequestHandler } from 'http-pro
42
46
43
47
const app = express ();
44
48
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
+ );
46
53
app .listen (3000 );
47
54
55
+ // proxy and keep the same base path "/api"
48
56
// http://localhost:3000/api/foo/bar -> http://www.example.org/api/foo/bar
49
57
```
50
58
@@ -57,7 +65,7 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
57
65
<!-- // spell-checker:disable -->
58
66
59
67
- [ Install] ( #install )
60
- - [ Core concept ] ( #core-concept )
68
+ - [ Basic usage ] ( #basic-usage )
61
69
- [ Express Server Example] ( #express-server-example )
62
70
- [ app.use(path, proxy)] ( #appusepath-proxy )
63
71
- [ Options] ( #options )
@@ -87,24 +95,23 @@ _All_ `http-proxy` [options](https://github.com/nodejitsu/node-http-proxy#option
87
95
npm install --save-dev http-proxy-middleware
88
96
```
89
97
90
- ## Core concept
98
+ ## Basic usage
91
99
92
100
Create and configure a proxy middleware with: ` createProxyMiddleware(config) ` .
93
101
94
102
``` javascript
95
103
const { createProxyMiddleware } = require (' http-proxy-middleware' );
96
104
97
105
const apiProxy = createProxyMiddleware ({
98
- pathFilter: ' /api' ,
99
106
target: ' http://www.example.org' ,
107
+ changeOrigin: true ,
100
108
});
101
109
102
110
// 'apiProxy' is now ready to be used as middleware in a server.
103
111
```
104
112
105
- - ** options.pathFilter** : Determine which requests should be proxied to the target host.
106
- (more on [ path filter] ( #path-filter ) )
107
113
- ** options.target** : target host to proxy to. _ (protocol + host)_
114
+ - ** options.changeOrigin** : for virtual hosted sites
108
115
109
116
- see full list of [ ` http-proxy-middleware ` configuration options] ( #options )
110
117
@@ -117,28 +124,19 @@ An example with `express` server.
117
124
const express = require (' express' );
118
125
const { createProxyMiddleware } = require (' http-proxy-middleware' );
119
126
127
+ const app = express ();
128
+
120
129
// proxy middleware options
121
130
/** @type {import('http-proxy-middleware/dist/types').Options} */
122
131
const options = {
123
- target: ' http://www.example.org' , // target host
132
+ target: ' http://www.example.org/api ' , // target host with the same base path
124
133
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
- },
135
134
};
136
135
137
136
// create the proxy
138
137
const exampleProxy = createProxyMiddleware (options);
139
138
140
139
// mount `exampleProxy` in web server
141
- const app = express ();
142
140
app .use (' /api' , exampleProxy);
143
141
app .listen (3000 );
144
142
```
@@ -149,7 +147,13 @@ If you want to use the server's `app.use` `path` parameter to match requests.
149
147
Use ` pathFilter ` option to further include/exclude requests which you want to proxy.
150
148
151
149
``` 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
+ );
153
157
```
154
158
155
159
` app.use ` documentation:
@@ -164,20 +168,11 @@ http-proxy-middleware options:
164
168
165
169
### ` pathFilter ` (string, [ ] string, glob, [ ] glob, function)
166
170
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.
177
172
178
173
- ** path matching**
179
174
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 .
181
176
- ` createProxyMiddleware({ pathFilter: '/api', ...}) ` - matches paths starting with ` /api `
182
177
183
178
- ** multiple path matching**
@@ -205,12 +200,13 @@ Decide which requests should be proxied; In case you are not able to use the ser
205
200
/**
206
201
* @return {Boolean}
207
202
*/
208
- const filter = function (path , req ) {
203
+ const pathFilter = function (path , req ) {
209
204
return path .match (' ^/api' ) && req .method === ' GET' ;
210
205
};
211
206
212
- const apiProxy = createProxyMiddleware (filter, {
207
+ const apiProxy = createProxyMiddleware ({
213
208
target: ' http://www.example.org' ,
209
+ pathFilter: pathFilter,
214
210
});
215
211
```
216
212
0 commit comments