Skip to content

Commit

Permalink
Example and README
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Feb 11, 2018
1 parent d424dce commit a799da3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Matteo Collina
Copyright (c) 2018 Matteo Collina

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
52 changes: 52 additions & 0 deletions README.md
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
49 changes: 49 additions & 0 deletions example.js
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()
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"devDependencies": {
"fastify": "^1.0.0-rc.1",
"got": "^8.0.3",
"make-promises-safe": "^1.1.0",
"pre-commit": "^1.2.2",
"simple-get": "^2.7.0",
"snazzy": "^7.0.0",
Expand Down

0 comments on commit a799da3

Please sign in to comment.