a simple wrapper around express-http-proxy
ensure Node is available, then install dprox:
$ npm install dprox
create a proxy.config.js
:
module.exports = {
self: "localhost:8080",
"/foo": {
uri: "localhost:8081",
preserveHost: true,
preservePrefix: true
},
"/bar": {
uri: "localhost:8082",
preserveHost: true
},
"/assets": "localhost:3000"
};
dprox
starts the proxy, reading configuration from proxy.config.js
within
the current working directory
dprox -c /path/to/any.js
reads a custom configuration file instead
(note that this assumes the dprox
executable resides on your PATH
, otherwise
you might have to provide the full path to that file)
proxy.config.js
is expected to export an object mapping paths to applications
an optional self
entry defines the proxy's own address (which defaults to
"localhost:3333"
) - this is either a string or a { uri, limit }
object
(limit
sets the request body size limit - see
express-http-proxy documentation
for details)
each entry is either a URI string, an Express middleware function (see below) or an object with the following options:
uri
is the address to pass requests topreserveHost: true
passes the HTTPHost
header through to the respective applicationpreservePrefix: true
passes the entry's path (URI prefix) through to the respective applicationinsecure: true
skips verification of SSL/TLS certificatesrequestHeaders
: an object of custom headers to add to any incoming request (e.g.{ "X-TOKEN": "abc123" }
) - these are added to and take precedence over any existing request headersresponseHeaders
: an object of custom headers to add to any outgoing response (e.g.{ "Cache-Control": "max-age=1" }
) - these are added to and take precedence over any existing response headerslog
, if truthy, activates logging for this entry- if the value is a function, it will be invoked with the respective HTTP
request object (e.g.
log: req => { console.log(req.method + req.url); }
) - otherwise the value, unless
true
, will be prepended to the default log message (e.g.log: "[PROXY]"
)
- if the value is a function, it will be invoked with the respective HTTP
request object (e.g.
middleware functions to serve static files from a corresponding directory or to mock a JSON response might look like this:
"/assets": require("express").static("static"),
"/data": (req, res, next) => {
let data = {
foo: "hello",
bar: "world"
};
return res.json(data);
}
npm install
downloads dependenciesnpm test
checks code for stylistic consistency