@ianwalter/relay

A flexible API to forward HTTP requests to another server

Stats

StarsIssuesVersionUpdatedCreatedSize
@ianwalter/relay
0113.1.02 years ago3 years agoMinified + gzip package size for @ianwalter/relay in KB

Readme

relay

A flexible API to forward HTTP requests to another server

npm page

Installation

yarn add @ianwalter/relay

Usage

Express setup example:

const Relay = require('@ianwalter/relay')

// Create the Relay instance and save it to the Express app instance so that
// route handlers will be able to access it when called.
const app = express()
app.locals.relay = new Relay({ baseUrl: process.env.API_URL })

Express simple proxy example:

const Relay = require('@ianwalter/relay')

// Proxy all requests to /api/account to /api/account on the baseUrl setup in
// the example above.
app.post('/api/account', Relay.proxy())

Express advanced proxy example:

const Relay = require('@ianwalter/relay')

// Pass the addAuthHeader function into the proxy options so that the authToken
// in the user's session is passed as an Authorization header when proxying
// requests to the other server.
const addAuthHeader = req => ({
  headers: { 'Authorization': req.session.authToken }
})
app.put('/api/account', Relay.proxy(addAuthHeader))

License

Apache 2.0 with Commons Clause - See LICENSE

 

Created by Ian Walter

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.