Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Aug 3, 2010
2 parents d0ad931 + 15c18b6 commit 93505a4
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 135 deletions.
File renamed without changes.
80 changes: 59 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,77 @@ Let's suppose you were running multiple http application servers, but you only w


### Installing npm (node package manager)

curl https://npmjs.org/install.sh | sh
<pre>
curl https://npmjs.org/install.sh | sh
</pre>

### Installing node-http-proxy
<pre>
npm install http-proxy
</pre>

npm install http-proxy

### How to setup a basic proxy server
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

### How to use node-http-proxy
httpProxy.createServer('9000', 'localhost').listen(8000);

var sys = require('sys'),
colors = require('colors'),
http = require('http'),
httpProxy = require('http-proxy').httpProxy;
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
</pre>

http.createServer(function (req, res){
var proxy = new httpProxy;
proxy.init(req, res);
proxy.proxyRequest('localhost', '9000', req, res);
}).listen(8000);
see the [demo](https://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
### How to setup a proxy server with custom server logic
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

// create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) {
// Put your custom server logic here
proxy.proxyRequest('9000', 'localhost', req, res);
}).listen(8000);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

</pre>

### How to proxy requests with a regular http server
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

// create a regular http server and proxy its handler
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);
// Put your custom server logic here
proxy.proxyRequest(9000, 'localhost', req, res);
}).listen(8001);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

</pre>

see the [demo](https://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
### Why doesn't node-http-proxy have more advanced features like x, y, or z?

if you have a suggestion for a feature currently not supported, feel free to open a [support issue](https://github.com/nodejitsu/node-http-proxy/issues). node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy
If you have a suggestion for a feature currently not supported, feel free to open a [support issue](https://github.com/nodejitsu/node-http-proxy/issues). node-http-proxy is designed to just proxy http requests from one server to another, but we will be soon releasing many other complimentary projects that can be used in conjunction with node-http-proxy.

<br/><br/><br/><br/><br/>

### License

(The MIT License)
Expand Down
32 changes: 19 additions & 13 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
var sys = require('sys'),
colors = require('colors')
http = require('http'),
httpProxy = require('http-proxy').httpProxy;
httpProxy = require('./lib/node-http-proxy');

// ascii art from https://github.com/marak/asciimo
var welcome = '\
Expand All @@ -39,25 +39,31 @@ var welcome = '\
# # # # # # # # #### # # # \n';
sys.puts(welcome.rainbow.bold);

// create regular http proxy server
http.createServer(function (req, res){
var proxy = new httpProxy;
proxy.init(req, res);
proxy.proxyRequest('localhost', '9000', req, res);
}).listen(8000);

/****** basic http proxy server ******/
httpProxy.createServer(9000, 'localhost').listen(8000);
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);

// http proxy server with latency
http.createServer(function (req, res){
var proxy = new (httpProxy);
proxy.init(req, res);
/****** http proxy server with latency******/
httpProxy.createServer(function (req, res, proxy){
setTimeout(function(){
proxy.proxyRequest('localhost', '9000', req, res);
proxy.proxyRequest(9000, 'localhost', req, res);
}, 200)
}).listen(8001);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );

// create regular http server
/****** http server with proxyRequest handler and latency******/
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);

setTimeout(function(){
proxy.proxyRequest(9000, 'localhost', req, res);
}, 200);
}).listen(8002);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);

/****** regular http server ******/
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
Expand Down
115 changes: 76 additions & 39 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,92 @@ var sys = require('sys'),
http = require('http'),
events = require('events');

exports.httpProxy = function () {
exports.HttpProxy = function () {
this.emitter = new(events.EventEmitter);
// If we were passed more than two arguments,
// assume the first two are request and response.
if(arguments.length >= 2) {
this.init(arguments[0], arguments[1]);
}
this.events = {};
this.listeners = {};
};

exports.createServer = function(callback){
sys.puts('httpProxy.createServer');
this.listen = function(host, port){
sys.puts(host + port);
};
return this;
exports.createServer = function () {
// Initialize the nodeProxy to start proxying requests
var proxy = new (exports.HttpProxy);
return proxy.createServer.apply(proxy, arguments);
};


exports.httpProxy.prototype = {
init: function (req, res) {
this.events = [];
exports.HttpProxy.prototype = {
toArray: function (obj){
var len = obj.length,
arr = new Array(len);
for (var i = 0; i < len; ++i) {
arr[i] = obj[i];
}
return arr;
},

createServer: function () {
var self = this,
server,
port,
callback;

if (typeof(arguments[0]) === "function") {
callback = arguments[0];
}
else {
port = arguments[0];
server = arguments[1];
}

var proxyServer = http.createServer(function (req, res){
self.watch(req, res);

// If we were passed a callback to process the request
// or response in some way, then call it.
if(callback) {
callback(req, res, self);
}
else {
self.proxyRequest(port, server, req, res);
}
});

return proxyServer;
},

watch: function (req, res) {
var self = this;

this.onData = function () {
self.events.push(['data'].concat(self.toArray(arguments)));
};
this.onEnd = function () {
self.events.push(['end'].concat(self.toArray(arguments)));
this.events[req] = [];

this.listeners[req] = {
onData: function () {
self.events[req].push(['data'].concat(self.toArray(arguments)));
},
onEnd: function () {
self.events[req].push(['end'].concat(self.toArray(arguments)));
}
};

req.addListener('data', this.onData);
req.addListener('end', this.onEnd);
req.addListener('data', this.listeners[req].onData);
req.addListener('end', this.listeners[req].onEnd);
},

unwatch: function (req, res) {
req.removeListener('data', this.listeners[req].onData);
req.removeListener('end', this.listeners[req].onEnd);

// Rebroadcast any events that have been buffered
while(this.events[req].length > 0) {
var args = this.events[req].shift();
req.emit.apply(req, args);
}

// Remove the data from the event and listeners hashes
delete this.listeners[req];
delete this.events[req];
},

proxyRequest: function (server, port, req, res) {
proxyRequest: function (port, server, req, res) {
// Remark: nodeProxy.body exists solely for testability
this.body = '';
var self = this;
Expand Down Expand Up @@ -116,20 +167,6 @@ exports.httpProxy.prototype = {
reverse_proxy.end();
});

req.removeListener('data', this.onData);
req.removeListener('end', this.onEnd);

// Rebroadcast any events that have been buffered
for (var i = 0, len = this.events.length; i < len; ++i) {
req.emit.apply(req, this.events[i]);
}
},
toArray: function (obj){
var len = obj.length,
arr = new Array(len);
for (var i = 0; i < len; ++i) {
arr[i] = obj[i];
}
return arr;
this.unwatch(req, res);
}
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "http-proxy",
"description": "A full-featured http reverse proxy for node.js",
"version": "0.1.3",
"version": "0.1.4",
"author": "Charlie Robbins <[email protected]>",
"contributors": [
{ "name": "Marak Squires", "email": "[email protected]" }
Expand Down
Loading

0 comments on commit 93505a4

Please sign in to comment.