json-http-proxy
v1.0.0
Published
A json configuration based http proxy server
Downloads
39
Readme
Configurable Proxy Server
An extensible proxy server which uses a basic JSON schema to configure all of its functionality. At its core, this is a wrapper
around node-http-proxy
and the Express router.
The routing determines which upstreams any given request will be proxied to.
The original goal of this project was to run in development when working with microservices. So you can run all the services localy and use this proxy on port 80 to point different routes to your services. Since then I have expanded the functionality to be a simple to use "smart" proxy, which you can use to to a bunch of things, including:
- Websocket proxying
- CORS headers
- Modifying request and response body data
- User-agent detection/modification (Ex. serving mobile website views)
- Cookie modifications
- Cacheing proxy
All the functionality for doing this kind of processing is done through a simple plugin system. Plugins are just a
function which are passed the instance of ProxyServer
and the config options. They can use this instance to hook into events used to modify
properties the requests, responses and other functionality. Even some core functionality is implemented as plugins, there are a few built in plugins for some common needs:
- CORS: Properly sets the
Access-Control-Allow-Methods
andAccess-Control-Allow-Origin
headers based on the incoming request, also responds toOPTIONS
requests. - Cookie modifier: Specify rules for modifying cookie names and values
Usage
$ npm install -g json-http-proxy
$ json-http-proxy
This will load the configuration file called proxy-config.json
. Here is an example of this file:
{
"routes": [
{
"path": "/api/users/*",
"upstream": "api-users"
},
{
"path": "/api/venues/*",
"upstream": "api-venues"
},
{
"path": "/api/broadcasts/*",
"upstream": "api-broadcasts"
},
{
"upstream": "client-website"
}
],
"upstreams": {
"client-website": {
"port": 3000
},
"api-users": {
"port": 3001
},
"api-venues": {
"port": 3002
},
"api-broadcasts": {
"port": 3003
}
}
}
This file is required in using node's require
method, so anything that is valid to require can be used here, so this can be javascript.
The above configuration file defined 4 upstream services which can be mapped to respond do different routes. The routing uses the Express router,
so see their docs for how to specify paths or use the express route tester.
Options
address
: the server address to listen onport
: port to open the server onstrict
: passed to the router, enables strict routingcaseSensitive
: passed to the router, enables case sensitive routingchangeOrigin
: passed tohttp-proxy
, enables origin changing in the proxyxfwd
: passed tohttp-proxy
, enablesx-forwarded-for
headers in the proxyheaders
: passed tohttp-proxy
, sets headers to be passed to all upstreamsssl
: passed tohttp-proxy
, sets ssl keys to use in an https servertimeout
: passed tohttp-proxy
asproxyTimeout
plugins
: an object of plugins to initalize where the plugin name is the keyupstreams
: an object of upstreams to register where the upstream name is the keyroutes
: an array of routes to handle