Code Monkey home page Code Monkey logo

http-json-proxy's Introduction

http-json-proxy

Build Status bitHound Overall Score bitHound Dependencies bitHound Code

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.

Installation

$ npm install --global http-json-proxy

Usage

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}

Options

$ 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]

API

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)

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.

options.port

Is the port the proxy will listen on. If not specified, the proxy will start listening to a random unused port.

options.host

Is the host the proxy will listen on. If not specified, the proxy will listen in all interfaces.

options.target

Is the proxied API server URL.

options.onReq

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.

options.onRes

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.

options.onErr

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.

License

MIT

http-json-proxy's People

Contributors

gabmontes avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

tonefreqhz

http-json-proxy's Issues

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.