-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,54 @@ | ||
# fastify-http-proxy | ||
|
||
Proxy your http requests to another server, with hooks. | ||
This [`fastify`](https://www.fastify.io) plugin forward all the request | ||
received with a given prefix (or none) to an upstream. All Fastify hooks | ||
are still applied. | ||
|
||
`fastify-http-proxy` is built on top of | ||
[`fastify-reply-from`](http://npm.im/fastify-reply-from), which enables | ||
you for single route proxying. | ||
|
||
## Install | ||
|
||
``` | ||
npm i fastify-http-proxy fastify | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
const Fastify = require('fastify') | ||
const server = Fastify() | ||
|
||
server.register(proxy, { | ||
upstream, | ||
prefix: '/upstream' // optional | ||
}) | ||
|
||
server.listen(3000) | ||
``` | ||
|
||
For a more complete example, 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. | ||
|
||
### upstream | ||
|
||
The target server to use for proxying | ||
|
||
### prefix | ||
|
||
The prefix to mount this plugin on. This is provided by fastify itself. | ||
|
||
## TODO | ||
|
||
* [ ] Generate unique request ids and implement request tracking | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const Fastify = require('fastify') | ||
const proxy = require('.') | ||
|
||
async function startOrigin () { | ||
const origin = Fastify() | ||
origin.get('/', async (request, reply) => { | ||
return 'this is root' | ||
}) | ||
|
||
origin.get('/a', async (request, reply) => { | ||
return 'this is a' | ||
}) | ||
|
||
origin.post('/this-has-data', async (request, reply) => { | ||
if (request.body.hello === 'world') { | ||
return { something: 'posted' } | ||
} | ||
throw new Error('kaboom') | ||
}) | ||
|
||
await origin.listen(0) | ||
|
||
return origin | ||
} | ||
|
||
async function startProxy (upstream) { | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream, | ||
prefix: '/upstream' // optional | ||
}) | ||
|
||
await server.listen(3000) | ||
} | ||
|
||
async function run () { | ||
const origin = await startOrigin() | ||
const upstream = `http://localhost:${origin.server.address().port}` | ||
|
||
console.log('origin started', upstream) | ||
|
||
const proxy = await startProxy(upstream) | ||
|
||
console.log('proxy started', `http://localhost:${proxy.server.address().port}/upstream/`) | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters