|
| 1 | +# http-json-proxy |
| 2 | + |
| 3 | +[](https://travis-ci.org/bloq/http-json-proxy) |
| 4 | +[](https://www.bithound.io/github/bloq/http-json-proxy) |
| 5 | +[](https://www.bithound.io/github/bloq/http-json-proxy/master/dependencies/npm) |
| 6 | +[](https://www.bithound.io/github/bloq/http-json-proxy) |
| 7 | + |
| 8 | +Simple HTTP JSON proxy. |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +```bash |
| 15 | +$ npm install --global http-json-proxy |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +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: |
| 21 | + |
| 22 | +``` |
| 23 | +$ http-json-proxy -p 18545 -t http://localhost:8545 |
| 24 | +Proxy for http://localhost:8545 listening on port 18545 |
| 25 | +``` |
| 26 | + |
| 27 | +Then, each call will be logged as follows: |
| 28 | + |
| 29 | +``` |
| 30 | +--> POST / {"jsonrpc":"2.0","id":3,"method":"eth_gasPrice","params":[]} |
| 31 | +<-- {"jsonrpc":"2.0","result":"0x2e90edd000","id":3} |
| 32 | +``` |
| 33 | + |
| 34 | +### Options |
| 35 | + |
| 36 | +``` |
| 37 | +$ http-json-proxy |
| 38 | +Start a HTTP JSON proxy server. |
| 39 | +
|
| 40 | +Options: |
| 41 | + --version Show version number [boolean] |
| 42 | + --port, -p the port the server should listen to [number] |
| 43 | + --target, -t the proxied API server URL [string] [required] |
| 44 | + --help Show help [boolean] |
| 45 | +``` |
| 46 | + |
| 47 | +## API |
| 48 | + |
| 49 | +The module can also be used programmatically as follows: |
| 50 | + |
| 51 | +```js |
| 52 | +const createProxy = require('http-json-proxy') |
| 53 | + |
| 54 | +const options = { |
| 55 | + port: 18545, |
| 56 | + target: 'http://localhost:8545', |
| 57 | + onReq: function (req) { |
| 58 | + console.log('-->', req.method, req.url, JSON.stringify(req.body)) |
| 59 | + return req |
| 60 | + }, |
| 61 | + onRes: function (body) { |
| 62 | + console.log('<--', JSON.stringify(body)) |
| 63 | + return body |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +const proxy = createProxy(options) |
| 68 | +``` |
| 69 | + |
| 70 | +### `createProxy(options)` |
| 71 | + |
| 72 | +Creates a new proxy that starts listening on the specified port, forwarding all requests to the target server. It returns an [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) instance. |
| 73 | + |
| 74 | +#### `options.port` |
| 75 | + |
| 76 | +Is the port the proxy will listen on. If not specified, the proxy will start listening to a random unused port. |
| 77 | + |
| 78 | +#### `options.host` |
| 79 | + |
| 80 | +Is the host the proxy will listen on. If not specified, the proxy will listen in all interfaces. |
| 81 | + |
| 82 | +#### `options.target` |
| 83 | + |
| 84 | +Is the proxied API server URL. |
| 85 | + |
| 86 | +#### `options.onReq` |
| 87 | + |
| 88 | +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. |
| 89 | + |
| 90 | +#### `options.onRes` |
| 91 | + |
| 92 | +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. |
| 93 | + |
| 94 | +#### `options.onErr` |
| 95 | + |
| 96 | +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. |
| 97 | + |
| 98 | +## License |
| 99 | + |
| 100 | +MIT |
0 commit comments