Skip to content

Commit 1f01156

Browse files
authored
Improved README.md
1 parent 4034126 commit 1f01156

File tree

1 file changed

+36
-11
lines changed

1 file changed

+36
-11
lines changed

README.md

Lines changed: 36 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 in your microservices architecture.
16+
1517
## Install
1618

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

2729
server.register(require('fastify-http-proxy'), {
28-
upstream,
29-
prefix: '/upstream', // optional
30+
'http://my-api.example.com',
31+
prefix: '/api', // optional
3032
http2: false // optional
3133
})
3234

3335
server.listen(3000)
3436
```
3537

36-
For a more complete example, see `example.js`.
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+
'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`.
3763

3864
## Options
3965

4066
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.*
4370

4471
### upstream
4572

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

4875
### prefix
4976

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

5279
### beforeHandler
5380

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

5783
### http2
5884

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

6287
## Benchmarks
6388

0 commit comments

Comments
 (0)