GithubHelp home page GithubHelp logo

config-proxy's Introduction

config-proxy

A simple library that enables the proxying on HTTP requests based on a simple configuration.

Example using the library:

var Router = require('config-proxy')
var http = require('http')

var router = Router({
  routes:{
    '/v1/projects':'http://127.0.0.1:8089'
  },
  'default':'/v1/projects'
})

var server = http.createServer(router)
server.listen(args.port)

Example using the CLI tool:

$ sudo npm install -g config-proxy

A file living in /etc/config-proxy.json:

{
  "routes":{
    "/v1/gui":"env:GUI_URL",
    "/v1/library":"env:LIBRARY_URL",
    "/v1/projects":"env:PROJECTS_URL"
  },
  "default":"/v1/gui"
}

We run the config-proxy HTTP server pointing at our config file and listening on port 8080:

$ config-proxy --config /etc/config-proxy.json --port 8080

install

$ npm install config-proxy

test

$ npm test

Config

  • routes - maps URLs onto backends
  • default - which route to use if there is no match

The value of the routes can be in env:VARNAME format. This means load the value of this environment variable as the backend route.

routes()

This function will return the processed routes back for analysis:

var routes = router.routes()

console.log(routes['/v1/projects'])

URL re-writing

Let's say that you proxy /v1/projects back to the projects micro-service.

Imaging that this micro-service just hosts '/v1/project/apples' (i.e. without projects)

This would mean we want the following URL re-writing to happen:

/v1/projects/project/apples -> /v1/project/apples

This can be done by appending /<route> after the proxy target in the config:

{
  routes:{
    // /v1/projects/project/apples -> http://127.0.0.1:8089/v1/project/apples
    '/v1/projects':'http://127.0.0.1:8089/v1'
  },
  'default':'/v1/projects'
}

Licence

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.