From b1eb13eb70b67ea76f5ab720d566894677a53ca2 Mon Sep 17 00:00:00 2001 From: Marak Squires Date: Tue, 27 Jul 2010 17:26:13 -0400 Subject: [PATCH] added createServer but hated it, gonna remove --- README.md | 15 ++++++++++++--- demo.js | 25 ++++++++++++++++++------- lib/node-http-proxy.js | 32 ++++++++++++++++++++------------ test/node-http-proxy-test.js | 6 +++--- 4 files changed, 53 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4a3201f..d6771b8 100644 --- a/README.md +++ b/README.md @@ -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 -
-  npm install http-proxy
-
+ npm install http-proxy ### How to use node-http-proxy + +#### usage 1:   creating a stand-alone proxy server + +#### usage 2:   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 \ No newline at end of file diff --git a/demo.js b/demo.js index 10dad12..8358758 100644 --- a/demo.js +++ b/demo.js @@ -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 * */ @@ -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 = {}; @@ -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); @@ -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'); \ No newline at end of file diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index 882cc8a..61bf9de 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -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); @@ -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; @@ -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 = ''; @@ -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; } }; diff --git a/test/node-http-proxy-test.js b/test/node-http-proxy-test.js index 8584abe..74c20fa 100644 --- a/test/node-http-proxy-test.js +++ b/test/node-http-proxy-test.js @@ -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 = {}; // @@ -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); @@ -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);