1.0.0 • Published 8 years ago

express-proxy-router v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
8 years ago

express-proxy-router

A router to proxy api requests (json/xml) through.

Usage (see src/example for another)

import express from 'express';
import proxy from '../index';

const server = express();

// Serves the result of api.test.com at the /test route
server.use(proxy({
  routes: [
    {
      path: '/test',
      proxy: {
        url: 'http://api.test.com',
        isXML: false
      }
    }
  ]
}));

server.get('/', (req, res) => {
  res.send('Hello World!');
});

server.listen(3000, () => {
  console.log('Server for testing the proxy is listening on port 3000!');
});

Proxy Options

OptionsDescriptionDefaultType
routesAn array of the routes to proxy for-Object

Proxy Routes Options

Proxy RoutesDescriptionDefaultType
pathThe express path to route for-String
proxyInfo on the proxynullObject
-proxy.urlThe url to get data for the proxynullString
-proxy.isXMLIs the data xmlfalseBoolean
-proxy.timeoutThe request timeout for server calls (ms)15000Number
headersOptions for the response header (same that express' res.setHeader takes)-Object

A note on path and proxy.url

We are using lodash.template to pass in the req params into the route. To capitalize on this write the path and proxy.url as so:

{
  path: '/test/:testParam',
  proxy: {
    url: 'https://api.test.com/<%= testParam %>'
  }