node-http-proxy is an HTTP programmable proxying library that supports
websockets. It is suitable for implementing components such as
proxies and load balancers.
A new proxy is created by calling
createProxyServer and passing
options object as argument (valid properties are available here)
var httpProxy = ;var proxy = httpProxy;
An object will be returned with four values:
req, res, [options](used for proxying regular HTTP(S) requests)
req, socket, head, [options](used for proxying WS(S) requests)
port(a function that wraps the object in a webserver, for your convenience)
Is it then possible to proxy requests by calling these functions
Errors can be listened on either using the Event Emitter API
or using the callback API
When a request is proxied it follows two different pipelines (available here)
which apply transformations to both the
The first pipeline (ingoing) is responsible for the creation and manipulation of the stream that connects your client to the target.
The second pipeline (outgoing) is responsible for the creation and manipulation of the stream that, from your target, returns data
to the client.
Setup a basic stand-alone proxy server
var http =httpProxy = ;//// Create your proxy server and set the target in the options.//httpProxy;//// Create your target server//http;
Setup a stand-alone proxy server with custom server logic
This example show how you can proxy a request using your own HTTP server and also you can put your own logic to handle the request.
var http =httpProxy = ;//// Create a proxy server with custom application logic//var proxy = httpProxy;//// Create your custom server and just call `proxy.web()` to proxy// a web request to the target passed in the options// also you can use `proxy.ws()` to proxy a websockets request//var server =;consoleserver;
Setup a stand-alone proxy server with latency
var http =httpProxy = ;//// Create a proxy server with latency//var proxy = httpProxy;//// Create your server that make an operation that take a while// and then proxy de request//http;//// Create your target server//http;
Listening for proxy events
error: The error event is emitted if the request to the target fail.
proxyRes: This event is emitted if the request to the target got a response.
var httpProxy = ;// Error example//// Http Proxy Server with bad target//var proxy = httpProxy;proxy;//// Listen for the `error` event on `proxy`.proxy;//// Listen for the `proxyRes` event on `proxy`.//proxy;
You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set
secure: true in the options.
HTTPS -> HTTP
//// Create the HTTPS proxy server in front of a HTTP server//httpProxy;
HTTPS -> HTTPS
//// Create the proxy server listening on port 443//httpProxy;
You can activate the websocket support for the proxy using
ws:true in the options.
//// Create a proxy server for websockets//httpProxy;
Also you can proxy the websocket requests just calling the
ws(req, socket, head) method.
//// Setup our server to proxy standard HTTP requests//var proxy =target:host: 'localhost'port: 9015;var proxyServer = http;//// Listen to the `upgrade` event and proxy the// WebSocket requests as well.//proxyServer;proxyServer;
Contributing and Issues
- Search on Google/Github
- If you can't find anything, open an issue
- If you feel comfortable about fixing the issue, fork the repo
- Commit to your local branch (which must be different from
- Submit your Pull Request (be sure to include tests and update documentation)
httpProxy.createProxyServer supports the following options:
- target: url string to be parsed with the url module
- forward: url string to be parsed with the url module
- agent: object to be passed to http(s).request (see Node's https agent and http agent objects)
- secure: true/false, if you want to verify the SSL Certs
If you are using the
proxyServer.listen method, the following options are also applicable:
- ssl: object to be passed to https.createServer()
- ws: true/false, if you want to proxy websockets
- xfwd: true/false, adds x-forward headers
$ npm test
Logo created by Diego Pasquali
The MIT License (MIT)
Copyright (c) 2010 - 2013 Nodejitsu Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.