From 1f011569763e041e088ac425fd2fe085b7400347 Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sat, 14 Jul 2018 15:25:22 +0100 Subject: [PATCH 1/4] Improved README.md --- README.md | 47 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8b91587..b4c7bf4 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ received with a given prefix (or none) to an upstream. All Fastify hooks are sti [`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables you for single route proxying. +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. + ## Install ``` @@ -25,39 +27,62 @@ const Fastify = require('fastify') const server = Fastify() server.register(require('fastify-http-proxy'), { - upstream, - prefix: '/upstream', // optional + 'http://my-api.example.com', + prefix: '/api', // optional http2: false // optional }) server.listen(3000) ``` -For a more complete example, see `example.js`. +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`. + +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: + +```js +const Fastify = require('fastify') +const server = Fastify() +const proxy = require('fastify-http-proxy') + +server.register(proxy, { + 'http://my-api.example.com', + prefix: '/api', // optional + http2: false // optional +}) + +server.register(proxy, { + 'http://single-signon.example.com/auth', + prefix: '/auth', // optional + http2: false // optional +}) + +server.listen(3000) +``` + +For other examples, see `example.js`. ## Options This `fastify` plugin supports the following options. -Note that this plugin is fully encapsulated, and non-JSON payloads will -be streamed directly to the destination. + +*Note that this plugin is fully encapsulated, and non-JSON payloads will +be streamed directly to the destination.* ### upstream -The target server to use for proxying +An URL (including protocol) that represents the target server to use for proxying. ### prefix -The prefix to mount this plugin on. This is provided by fastify itself. +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. ### beforeHandler -A `beforeHandler` to be applied on all routes. Useful for performing -authentication. +A `beforeHandler` to be applied on all routes. Useful for performing actions before the proxy is executed (e.g. check for authentication). ### http2 -A `beforeHandler` to be applied on all routes. Useful for performing -authentication. +A boolean value that indicates whether the proxy should support http2. ## Benchmarks From 87fa7faf5bbed90165039af8a4f5ec1009698c08 Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sat, 14 Jul 2018 15:31:15 +0100 Subject: [PATCH 2/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b4c7bf4..860dd7f 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ received with a given prefix (or none) to an upstream. All Fastify hooks are sti [`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables you for single route proxying. -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 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. ## Install From 66c9437174629e7e5f7edb15394b3b0b003972e3 Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sun, 15 Jul 2018 10:49:16 +0100 Subject: [PATCH 3/4] Update README.md --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 860dd7f..7b71d7a 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ const Fastify = require('fastify') const server = Fastify() server.register(require('fastify-http-proxy'), { - 'http://my-api.example.com', + upstream: 'http://my-api.example.com', prefix: '/api', // optional http2: false // optional }) @@ -45,13 +45,13 @@ const server = Fastify() const proxy = require('fastify-http-proxy') server.register(proxy, { - 'http://my-api.example.com', + upstream: 'http://my-api.example.com', prefix: '/api', // optional http2: false // optional }) server.register(proxy, { - 'http://single-signon.example.com/auth', + upstream: 'http://single-signon.example.com/auth', prefix: '/auth', // optional http2: false // optional }) @@ -59,6 +59,8 @@ server.register(proxy, { server.listen(3000) ``` +Notice that in these case it is important to use the `prefix` option and to have *non-ambiguous* or *non-overlapping* prefixes (e.g. `/api` and `/api2`). + For other examples, see `example.js`. ## Options From 1d3f6b94623f16b789a79d5fd80eb1810db3b24a Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sun, 15 Jul 2018 11:42:48 +0100 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7b71d7a..47d872a 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ server.register(proxy, { server.listen(3000) ``` -Notice that in these case it is important to use the `prefix` option and to have *non-ambiguous* or *non-overlapping* prefixes (e.g. `/api` and `/api2`). +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. For other examples, see `example.js`.