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
Copy file name to clipboardexpand all lines: README.md
+51-46
Original file line number
Diff line number
Diff line change
@@ -27,58 +27,58 @@ npm i @fastify/http-proxy fastify
27
27
## Example
28
28
29
29
```js
30
-
constFastify=require('fastify')
31
-
constserver=Fastify()
30
+
constFastify=require('fastify');
31
+
constserver=Fastify();
32
32
33
33
server.register(require('@fastify/http-proxy'), {
34
34
upstream:'http://my-api.example.com',
35
35
prefix:'/api', // optional
36
-
http2:false// optional
37
-
})
36
+
http2:false,// optional
37
+
});
38
38
39
-
server.listen({ port:3000 })
39
+
server.listen({ port:3000 });
40
40
```
41
41
42
42
This will proxy any request starting with `/api` to `http://my-api.example.com`. For instance `http://localhost:3000/api/users` will be proxied to `http://my-api.example.com/users`.
43
43
44
44
If you want to have different proxies on different prefixes you can register multiple instances of the plugin as shown in the following snippet:
45
45
46
46
```js
47
-
constFastify=require('fastify')
48
-
constserver=Fastify()
49
-
constproxy=require('@fastify/http-proxy')
47
+
constFastify=require('fastify');
48
+
constserver=Fastify();
49
+
constproxy=require('@fastify/http-proxy');
50
50
51
51
// /api/x will be proxied to http://my-api.example.com/x
52
52
server.register(proxy, {
53
53
upstream:'http://my-api.example.com',
54
54
prefix:'/api', // optional
55
-
http2:false// optional
56
-
})
55
+
http2:false,// optional
56
+
});
57
57
58
58
// /rest-api/123/endpoint will be proxied to http://my-rest-api.example.com/123/endpoint
59
59
server.register(proxy, {
60
60
upstream:'http://my-rest-api.example.com',
61
61
prefix:'/rest-api/:id/endpoint', // optional
62
62
rewritePrefix:'/:id/endpoint', // optional
63
-
http2:false// optional
64
-
})
63
+
http2:false,// optional
64
+
});
65
65
66
66
// /auth/user will be proxied to http://single-signon.example.com/signon/user
67
67
server.register(proxy, {
68
68
upstream:'http://single-signon.example.com',
69
69
prefix:'/auth', // optional
70
70
rewritePrefix:'/signon', // optional
71
-
http2:false// optional
72
-
})
71
+
http2:false,// optional
72
+
});
73
73
74
74
// /user will be proxied to http://single-signon.example.com/signon/user
75
75
server.register(proxy, {
76
76
upstream:'http://single-signon.example.com',
77
77
rewritePrefix:'/signon', // optional
78
-
http2:false// optional
79
-
})
78
+
http2:false,// optional
79
+
});
80
80
81
-
server.listen({ port:3000 })
81
+
server.listen({ port:3000 });
82
82
```
83
83
84
84
Notice that in this case it is important to use the `prefix` option to tell the proxy how to properly route the requests across different upstreams.
@@ -92,20 +92,22 @@ For other examples, see [`example.js`](examples/example.js).
92
92
`@fastify/http-proxy` can track and pipe the `request-id` across the upstreams. Using the [`hyperid`](https://www.npmjs.com/package/hyperid) module and the [`@fastify/reply-from`](https://github.com/fastify/fastify-reply-from) built-in options a fairly simple example would look like this:
@@ -179,6 +181,7 @@ configuration passed to the route.
179
181
Object with [reply options](https://github.com/fastify/fastify-reply-from#replyfromsource-opts) for `@fastify/reply-from`.
180
182
181
183
### `internalRewriteLocationHeader`
184
+
182
185
By default, `@fastify/http-proxy` will rewrite the `location` header when a request redirects to a relative path.
183
186
In other words, the [prefix](https://github.com/fastify/fastify-http-proxy#prefix) will be added to the relative path.
184
187
@@ -187,12 +190,13 @@ If you want to preserve the original path, this option will disable this interna
187
190
Note that the [rewriteHeaders](https://github.com/fastify/fastify-reply-from#rewriteheadersheaders-request) option of [`@fastify/reply-from`](http://npm.im/fastify-reply-from) will retrieve headers modified (reminder: only `location` is updated among all headers) in parameter but with this option, the headers are unchanged.
188
191
189
192
### `httpMethods`
193
+
190
194
An array that contains the types of the methods. Default: `['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']`.
191
195
192
196
### `websocket`
193
197
194
198
This module has _partial_ support for forwarding websockets by passing a
195
-
`websocket` boolean option.
199
+
`websocket` boolean option.
196
200
197
201
A few things are missing:
198
202
@@ -203,6 +207,7 @@ A few things are missing:
203
207
Pull requests are welcome to finish this feature.
204
208
205
209
### `wsUpstream`
210
+
206
211
Working only if property `websocket` is `true`.
207
212
208
213
An URL (including protocol) that represents the target websockets to use for proxying websockets.
@@ -229,19 +234,19 @@ The default implementation forwards the `cookie` header.
229
234
230
235
The following benchmarks where generated on a dedicated server with an Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz and 64GB of RAM:
0 commit comments