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
+36-11Lines changed: 36 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 in your 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
+
'http://my-api.example.com',
49
+
prefix:'/api', // optional
50
+
http2:false// optional
51
+
})
52
+
53
+
server.register(proxy, {
54
+
'http://single-signon.example.com/auth',
55
+
prefix:'/auth', // optional
56
+
http2:false// optional
57
+
})
58
+
59
+
server.listen(3000)
60
+
```
61
+
62
+
For other examples, see `example.js`.
37
63
38
64
## Options
39
65
40
66
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.
67
+
68
+
*Note that this plugin is fully encapsulated, and non-JSON payloads will
69
+
be streamed directly to the destination.*
43
70
44
71
### upstream
45
72
46
-
The target server to use for proxying
73
+
An URL (including protocol) that represents the target server to use for proxying.
47
74
48
75
### prefix
49
76
50
-
The prefix to mount this plugin on. This is provided by fastify itself.
77
+
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
78
52
79
### beforeHandler
53
80
54
-
A `beforeHandler` to be applied on all routes. Useful for performing
55
-
authentication.
81
+
A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication).
56
82
57
83
### http2
58
84
59
-
A `beforeHandler` to be applied on all routes. Useful for performing
60
-
authentication.
85
+
A boolean value that indicates whether the proxy should support http2.
0 commit comments