Simple HTTP JSON proxy.
This proxy can be used as a middleman in between a HTTP JSON API server and a client to monitor the requests, responses and even modify on the fly any of those.
$ npm install --global http-json-proxy
The following command will spin up a proxy server that will forward all requests to a locally installed Ethereum node and will log to console each JSON RPC call with the corresponding response:
$ http-json-proxy -p 18545 -t http://localhost:8545
Proxy for http://localhost:8545 listening on port 18545
Then, each call will be logged as follows:
--> POST / {"jsonrpc":"2.0","id":3,"method":"eth_gasPrice","params":[]}
<-- {"jsonrpc":"2.0","result":"0x2e90edd000","id":3}
$ http-json-proxy
Start a HTTP JSON proxy server.
Options:
--version Show version number [boolean]
--port, -p the port the server should listen to [number]
--target, -t the proxied API server URL [string] [required]
--help Show help [boolean]
The module can also be used programmatically as follows:
const createProxy = require('http-json-proxy')
const options = {
port: 18545,
target: 'http://localhost:8545',
onReq: function (req) {
console.log('-->', req.method, req.url, JSON.stringify(req.body))
return req
},
onRes: function (body) {
console.log('<--', JSON.stringify(body))
return body
}
}
const proxy = createProxy(options)
Creates a new proxy that starts listening on the specified port, forwarding all requests to the target server. It returns an http.Server
instance.
Is the port the proxy will listen on. If not specified, the proxy will start listening to a random unused port.
Is the host the proxy will listen on. If not specified, the proxy will listen in all interfaces.
Is the proxied API server URL.
Will be called on each request with the req
object that will be forwarded to the target server and shall return that req
. Any of the properties of the req
object can be altered to modify the actual request that is sent to the target server. Defaults to the identity function.
Will be called on each response with the body
of the response and shall return the actual body
to be provided to the client. It can be altered to provide a different response too. Defaults to the identity function.
Will be called on each request error with the corresponding err
object and shall return the same, altered or different err
object that will be returned to the client along with a 500 status code. Defaults to the identity function.
MIT