digitalbazaar/node-http-proxy

Name: node-http-proxy

Owner: Digital Bazaar, Inc.

Description: A full-featured http proxy for node.js

Created: 2014-06-29 02:09:14.0

Updated: 2014-07-31 18:00:27.0

Pushed: 2014-07-18 15:46:30.0

Homepage: http://github.com/nodejitsu/node-http-proxy

Size: 1598

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

node-http-proxy

node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as proxies and load balancers.

Build Status

  

Looking to Upgrade from 0.8.x ? Click here
Core Concept

A new proxy is created by calling createProxyServer and passing an options object as argument (valid properties are available here)

httpProxy = require('http-proxy');

proxy = httpProxy.createProxyServer(options);

An object will be returned with four values:

Is it then possible to proxy requests by calling these functions

ire('http').createServer(function(req, res) {
oxy.web(req, res, { target: 'http://mytarget.com:8080' });

Errors can be listened on either using the Event Emitter API

y.on('error', function(e) {
.

or using the callback API

y.web(req, res, { target: 'http://mytarget.com:8080' }, function(e) { ... });

When a request is proxied it follows two different pipelines (available here) which apply transformations to both the req and res object. 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
http = require('http'),
httpProxy = require('http-proxy');

reate your proxy server and set the target in the options.

Proxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);


reate your target server

.createServer(function (req, res) {
s.writeHead(200, { 'Content-Type': 'text/plain' });
s.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
s.end();
isten(9000);
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.

http = require('http'),
httpProxy = require('http-proxy');


reate a proxy server with custom application logic

proxy = httpProxy.createProxyServer({});


reate your custom server and just call `proxy.web()` to proxy
 web request to the target passed in the options
lso you can use `proxy.ws()` to proxy a websockets request

server = require('http').createServer(function(req, res) {
 You can define here your custom logic to handle the request
 and then proxy the request.
oxy.web(req, res, { target: 'http://127.0.0.1:5060' });


ole.log("listening on port 5050")
er.listen(5050);
Setup a stand-alone proxy server with latency
http = require('http'),
httpProxy = require('http-proxy');


reate a proxy server with latency

proxy = httpProxy.createProxyServer();


reate your server that make an operation that take a while
nd then proxy de request

.createServer(function (req, res) {
 This simulate an operation that take 500ms in execute
tTimeout(function () {
proxy.web(req, res, {
  target: 'http://localhost:9008'
});
 500);
isten(8008);


reate your target server

.createServer(function (req, res) {
s.writeHead(200, { 'Content-Type': 'text/plain' });
s.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
s.end();
isten(9008);
Listening for proxy events
httpProxy = require('http-proxy');
rror example

ttp Proxy Server with bad target

proxy = httpProxy.createServer({
rget:'http://localhost:9005'


y.listen(8005);


isten for the `error` event on `proxy`.
y.on('error', function (err, req, res) {
s.writeHead(500, {
'Content-Type': 'text/plain'
;

s.end('Something went wrong. And we are reporting a custom error message.');



isten for the `proxyRes` event on `proxy`.

y.on('proxyRes', function (res) {
nsole.log('RAW Response from the target', JSON.stringify(res.headers, true, 2));

Using HTTPS

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

reate the HTTPS proxy server in front of a HTTP server

Proxy.createServer({
rget: {
host: 'localhost',
port: 9009

l: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')

isten(8009);
HTTPS -> HTTPS

reate the proxy server listening on port 443

Proxy.createServer({
l: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')

rget: 'https://localhost:9010',
cure: true // Depends on your needs, could be false.
isten(443);
Proxying WebSockets

You can activate the websocket support for the proxy using ws:true in the options.


reate a proxy server for websockets

Proxy.createServer({
rget: 'ws://localhost:9014',
: true
isten(8014);

Also you can proxy the websocket requests just calling the ws(req, socket, head) method.


etup our server to proxy standard HTTP requests

proxy = new httpProxy.createProxyServer({
rget: {
host: 'localhost',
port: 9015


proxyServer = http.createServer(function (req, res) {
oxy.web(req, res);



isten to the `upgrade` event and proxy the
ebSocket requests as well.

yServer.on('upgrade', function (req, socket, head) {
oxy.ws(req, socket, head);


yServer.listen(8015);
Contributing and Issues
Options

httpProxy.createProxyServer supports the following options:

If you are using the proxyServer.listen method, the following options are also applicable:

Test
m test
Logo

Logo created by Diego Pasquali

License

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.