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
+38-11Lines changed: 38 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,8 @@ received with a given prefix (or none) to an upstream. All Fastify hooks are sti
12
12
[`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables
13
13
you for single route proxying.
14
14
15
+
This plugin can be used in a variety of circumstances, for example if you have to proxy an internal domain to an external domain (useful to avoid CORS problems) or to implement your own API gateway for a microservices architecture.
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`.
39
+
40
+
If you want to have different proxies on different prefixes in you can register multiple instances of the plugin as shown in the following snippet:
41
+
42
+
```js
43
+
constFastify=require('fastify')
44
+
constserver=Fastify()
45
+
constproxy=require('fastify-http-proxy')
46
+
47
+
server.register(proxy, {
48
+
upstream:'http://my-api.example.com',
49
+
prefix:'/api', // optional
50
+
http2:false// optional
51
+
})
52
+
53
+
server.register(proxy, {
54
+
upstream:'http://single-signon.example.com/auth',
55
+
prefix:'/auth', // optional
30
56
http2:false// optional
31
57
})
32
58
33
59
server.listen(3000)
34
60
```
35
61
36
-
For a more complete example, see `example.js`.
62
+
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.
63
+
64
+
For other examples, see `example.js`.
37
65
38
66
## Options
39
67
40
68
This `fastify` plugin supports the following options.
41
-
Note that this plugin is fully encapsulated, and non-JSON payloads will
42
-
be streamed directly to the destination.
69
+
70
+
*Note that this plugin is fully encapsulated, and non-JSON payloads will
71
+
be streamed directly to the destination.*
43
72
44
73
### upstream
45
74
46
-
The target server to use for proxying
75
+
An URL (including protocol) that represents the target server to use for proxying.
47
76
48
77
### prefix
49
78
50
-
The prefix to mount this plugin on. This is provided by fastify itself.
79
+
The prefix to mount this plugin on. All the requests to the current server starting with the given prefix will be proxied to the provided upstream.
51
80
52
81
### beforeHandler
53
82
54
-
A `beforeHandler` to be applied on all routes. Useful for performing
55
-
authentication.
83
+
A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
56
84
57
85
### http2
58
86
59
-
A `beforeHandler` to be applied on all routes. Useful for performing
60
-
authentication.
87
+
A boolean value that indicates whether the proxy should support http2.
0 commit comments