Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including service_proxy with all npm packages installed. Try it out:

var serviceProxy = require("service_proxy")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

service_proxy v0.0.5

A small wrapper around request to handle proxying calls with nice error handling

#ServiceProxy

###What is it? A small wrapper around request (https://github.com/mikeal/request) that eases proxying services.

###Example

myApp proxies all calls to myService with the same URL scheme used except for all calls proxied to myService have a "/service" prepended to the url.

#myApp.coffee

app.get '/service/resource', serviceController.proxy
app.post '/service/resource', serviceController.proxy


#serviceController.coffee
ServiceProxy = require 'service_proxy'
serviceProxy = new ServiceProxy {
  host: "http://localhost:4001"
  transformUrl: (host, path) ->
    return host + path.replace "/service", ""
  onError: (req, res) ->
    res.send err.message, 500
}

module.exports.proxy = (req, res) ->
  serviceProxy.proxy req, res, { requestOpts: {json: true} }

API

Can either be used as a static method:

ServiceProxy.proxy req, res, opts

or as a class as in the example above

Options

host: the hostname to proxy to (is not required, can be done in transformUrl) transformUrl: a function that returns the route to the service, gets passed the host and request path onError: a function to run when a pipe error occurs requestOpts: a hash of options to be passed to request

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free