Skip to content

Commit

Permalink
added createServer but hated it, gonna remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Jul 27, 2010
1 parent c4b7c0d commit b1eb13e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
- written entirely in javascript
- easy to use api

### Todo
- add ability to black list ip addresses

### When to use node-http-proxy

Let's suppose you were running multiple http application servers, but you only wanted to expose one machine to the internet. You could setup node-http-proxy on that one machine and then reverse-proxy the incoming http requests to locally running services which were not exposed to the outside network.

### Installing node-http-proxy

<pre>
npm install http-proxy
</pre>
npm install http-proxy

### How to use node-http-proxy

#### usage 1:&nbsp;&nbsp;&nbsp;creating a stand-alone proxy server

#### usage 2:&nbsp;&nbsp;&nbsp;proxying existing http.Server requests

### 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 https request 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
25 changes: 18 additions & 7 deletions demo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* node-proxy-test.js: Tests for node-proxy. Reverse proxy for node.js
*
* (C) 2010 Charlie Robbins
* (C) 2010 Charlie Robbins, Marak Squires
* MIT LICENSE
*
*/
Expand All @@ -12,7 +12,7 @@ var vows = require('vows'),
assert = require('assert'),
http = require('http');

var HttpProxy = require('./lib/node-http-proxy').HttpProxy;
var httpProxy = require('./lib/node-http-proxy');
var testServers = {};


Expand All @@ -26,17 +26,30 @@ var welcome = '\
# # # # # # # # #### # # # \n';
sys.puts(welcome.rainbow.bold);


// create regular http proxy server
httpProxy.createServer('localhost', 9000, function (req, res){

sys.puts('any requests going to 8002 will get proxied to 9000');

}).listen('localhost', 8002);

sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);



// create regular http proxy server
http.createServer(function (req, res){
var proxy = new (HttpProxy);
var proxy = new httpProxy.httpProxy;
proxy.init(req, res);
sys.puts('proxying request to http://localhost:9000');
proxy.proxyRequest('localhost', '9000', req, res);
}).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);
var proxy = new (httpProxy);
proxy.init(req, res);
setTimeout(function(){
proxy.proxyRequest('localhost', '9000', req, res);
Expand All @@ -47,9 +60,7 @@ sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '
// create regular http server
http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('foo');
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
//sys.puts('to test the proxy server, request http://localhost:8080/ in your browser.');
//sys.puts('your request will proxy to the server running on port 8081');
32 changes: 20 additions & 12 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var sys = require('sys'),
http = require('http'),
events = require('events');

exports.HttpProxy = function () {
exports.httpProxy = function () {
var self = this;
this.emitter = new(events.EventEmitter);

Expand All @@ -21,16 +21,16 @@ exports.HttpProxy = function () {
}
};

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;
},
exports.createServer = function(callback){
sys.puts('httpProxy.createServer');
this.listen = function(host, port){
sys.puts(host + port);
};
return this;
};


exports.httpProxy.prototype = {
init: function (req, res) {
this.events = [];
var self = this;
Expand All @@ -45,7 +45,7 @@ exports.HttpProxy.prototype = {
req.addListener('data', this.onData);
req.addListener('end', this.onEnd);
},

proxyRequest: function (server, port, req, res) {
// Remark: nodeProxy.body exists solely for testability
this.body = '';
Expand Down Expand Up @@ -107,5 +107,13 @@ exports.HttpProxy.prototype = {
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;
}
};
6 changes: 3 additions & 3 deletions test/node-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var vows = require('vows'),

require.paths.unshift(require('path').join(__dirname, '../lib/'));

var HttpProxy = require('node-http-proxy').HttpProxy;
var httpProxy = require('node-http-proxy');
var testServers = {};

//
Expand Down Expand Up @@ -89,7 +89,7 @@ vows.describe('node-proxy').addBatch({
"When an incoming request is proxied to the helloNode server" : {
"with no latency" : {
topic: function () {
var proxy = new (HttpProxy);
var proxy = new httpProxy.httpProxy;
startTest(proxy, 8082);
proxy.emitter.addListener('end', this.callback);

Expand All @@ -106,7 +106,7 @@ vows.describe('node-proxy').addBatch({
},
"with latency": {
topic: function () {
var proxy = new (HttpProxy);
var proxy = new httpProxy.httpProxy;
startTestWithLatency(proxy, 8083);
proxy.emitter.addListener('end', this.callback);

Expand Down

0 comments on commit b1eb13e

Please sign in to comment.