Skip to content

Commit d52b2fc

Browse files
authored
Merge pull request #10 from lmammino/patch-1
Improved README.md
2 parents 63b577c + 1d3f6b9 commit d52b2fc

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

README.md

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ received with a given prefix (or none) to an upstream. All Fastify hooks are sti
1212
[`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables
1313
you for single route proxying.
1414

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.
16+
1517
## Install
1618

1719
```
@@ -25,39 +27,64 @@ const Fastify = require('fastify')
2527
const server = Fastify()
2628

2729
server.register(require('fastify-http-proxy'), {
28-
upstream,
29-
prefix: '/upstream', // optional
30+
upstream: 'http://my-api.example.com',
31+
prefix: '/api', // optional
32+
http2: false // optional
33+
})
34+
35+
server.listen(3000)
36+
```
37+
38+
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+
const Fastify = require('fastify')
44+
const server = Fastify()
45+
const proxy = 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
3056
http2: false // optional
3157
})
3258

3359
server.listen(3000)
3460
```
3561

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`.
3765

3866
## Options
3967

4068
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.*
4372

4473
### upstream
4574

46-
The target server to use for proxying
75+
An URL (including protocol) that represents the target server to use for proxying.
4776

4877
### prefix
4978

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.
5180

5281
### beforeHandler
5382

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).
5684

5785
### http2
5886

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.
6188

6289
## Benchmarks
6390

0 commit comments

Comments
 (0)