Skip to content

Commit 93505a4

Browse files
committed
merge
2 parents d0ad931 + 15c18b6 commit 93505a4

File tree

6 files changed

+207
-135
lines changed

6 files changed

+207
-135
lines changed

MIT-LICENSE renamed to LICENSE

File renamed without changes.

README.md

+59-21
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,77 @@ Let's suppose you were running multiple http application servers, but you only w
2121

2222

2323
### Installing npm (node package manager)
24-
25-
curl http://npmjs.org/install.sh | sh
24+
<pre>
25+
curl http://npmjs.org/install.sh | sh
26+
</pre>
2627

2728
### Installing node-http-proxy
29+
<pre>
30+
npm install http-proxy
31+
</pre>
2832

29-
npm install http-proxy
30-
33+
### How to setup a basic proxy server
34+
<pre>
35+
var http = require('http'),
36+
httpProxy = require('http-proxy');
3137

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

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

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

45-
http.createServer(function (req, res){
46-
res.writeHead(200, {'Content-Type': 'text/plain'});
47-
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
48-
res.end();
49-
}).listen(9000);
49+
### How to setup a proxy server with custom server logic
50+
<pre>
51+
var http = require('http'),
52+
httpProxy = require('http-proxy');
53+
54+
// create a proxy server with custom application logic
55+
httpProxy.createServer(function (req, res, proxy) {
56+
// Put your custom server logic here
57+
proxy.proxyRequest('9000', 'localhost', req, res);
58+
}).listen(8000);
59+
60+
http.createServer(function (req, res){
61+
res.writeHead(200, {'Content-Type': 'text/plain'});
62+
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
63+
res.end();
64+
}).listen(9000);
65+
66+
</pre>
67+
68+
### How to proxy requests with a regular http server
69+
<pre>
70+
var http = require('http'),
71+
httpProxy = require('http-proxy');
72+
73+
// create a regular http server and proxy its handler
74+
http.createServer(function (req, res){
75+
var proxy = new httpProxy.HttpProxy;
76+
proxy.watch(req, res);
77+
// Put your custom server logic here
78+
proxy.proxyRequest(9000, 'localhost', req, res);
79+
}).listen(8001);
80+
81+
http.createServer(function (req, res){
82+
res.writeHead(200, {'Content-Type': 'text/plain'});
83+
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
84+
res.end();
85+
}).listen(9000);
86+
87+
</pre>
5088

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

54-
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
91+
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.
5592

5693
<br/><br/><br/><br/><br/>
94+
5795
### License
5896

5997
(The MIT License)

demo.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
var sys = require('sys'),
2828
colors = require('colors')
2929
http = require('http'),
30-
httpProxy = require('http-proxy').httpProxy;
30+
httpProxy = require('./lib/node-http-proxy');
3131

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

42-
// create regular http proxy server
43-
http.createServer(function (req, res){
44-
var proxy = new httpProxy;
45-
proxy.init(req, res);
46-
proxy.proxyRequest('localhost', '9000', req, res);
47-
}).listen(8000);
42+
43+
/****** basic http proxy server ******/
44+
httpProxy.createServer(9000, 'localhost').listen(8000);
4845
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
4946

50-
// http proxy server with latency
51-
http.createServer(function (req, res){
52-
var proxy = new (httpProxy);
53-
proxy.init(req, res);
47+
/****** http proxy server with latency******/
48+
httpProxy.createServer(function (req, res, proxy){
5449
setTimeout(function(){
55-
proxy.proxyRequest('localhost', '9000', req, res);
50+
proxy.proxyRequest(9000, 'localhost', req, res);
5651
}, 200)
5752
}).listen(8001);
5853
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );
5954

60-
// create regular http server
55+
/****** http server with proxyRequest handler and latency******/
56+
http.createServer(function (req, res){
57+
var proxy = new httpProxy.HttpProxy;
58+
proxy.watch(req, res);
59+
60+
setTimeout(function(){
61+
proxy.proxyRequest(9000, 'localhost', req, res);
62+
}, 200);
63+
}).listen(8002);
64+
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
65+
66+
/****** regular http server ******/
6167
http.createServer(function (req, res){
6268
res.writeHead(200, {'Content-Type': 'text/plain'});
6369
res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));

lib/node-http-proxy.js

+76-39
Original file line numberDiff line numberDiff line change
@@ -28,41 +28,92 @@ var sys = require('sys'),
2828
http = require('http'),
2929
events = require('events');
3030

31-
exports.httpProxy = function () {
31+
exports.HttpProxy = function () {
3232
this.emitter = new(events.EventEmitter);
33-
// If we were passed more than two arguments,
34-
// assume the first two are request and response.
35-
if(arguments.length >= 2) {
36-
this.init(arguments[0], arguments[1]);
37-
}
33+
this.events = {};
34+
this.listeners = {};
3835
};
3936

40-
exports.createServer = function(callback){
41-
sys.puts('httpProxy.createServer');
42-
this.listen = function(host, port){
43-
sys.puts(host + port);
44-
};
45-
return this;
37+
exports.createServer = function () {
38+
// Initialize the nodeProxy to start proxying requests
39+
var proxy = new (exports.HttpProxy);
40+
return proxy.createServer.apply(proxy, arguments);
4641
};
4742

48-
49-
exports.httpProxy.prototype = {
50-
init: function (req, res) {
51-
this.events = [];
43+
exports.HttpProxy.prototype = {
44+
toArray: function (obj){
45+
var len = obj.length,
46+
arr = new Array(len);
47+
for (var i = 0; i < len; ++i) {
48+
arr[i] = obj[i];
49+
}
50+
return arr;
51+
},
52+
53+
createServer: function () {
54+
var self = this,
55+
server,
56+
port,
57+
callback;
58+
59+
if (typeof(arguments[0]) === "function") {
60+
callback = arguments[0];
61+
}
62+
else {
63+
port = arguments[0];
64+
server = arguments[1];
65+
}
66+
67+
var proxyServer = http.createServer(function (req, res){
68+
self.watch(req, res);
69+
70+
// If we were passed a callback to process the request
71+
// or response in some way, then call it.
72+
if(callback) {
73+
callback(req, res, self);
74+
}
75+
else {
76+
self.proxyRequest(port, server, req, res);
77+
}
78+
});
79+
80+
return proxyServer;
81+
},
82+
83+
watch: function (req, res) {
5284
var self = this;
5385

54-
this.onData = function () {
55-
self.events.push(['data'].concat(self.toArray(arguments)));
56-
};
57-
this.onEnd = function () {
58-
self.events.push(['end'].concat(self.toArray(arguments)));
86+
this.events[req] = [];
87+
88+
this.listeners[req] = {
89+
onData: function () {
90+
self.events[req].push(['data'].concat(self.toArray(arguments)));
91+
},
92+
onEnd: function () {
93+
self.events[req].push(['end'].concat(self.toArray(arguments)));
94+
}
5995
};
6096

61-
req.addListener('data', this.onData);
62-
req.addListener('end', this.onEnd);
97+
req.addListener('data', this.listeners[req].onData);
98+
req.addListener('end', this.listeners[req].onEnd);
99+
},
100+
101+
unwatch: function (req, res) {
102+
req.removeListener('data', this.listeners[req].onData);
103+
req.removeListener('end', this.listeners[req].onEnd);
104+
105+
// Rebroadcast any events that have been buffered
106+
while(this.events[req].length > 0) {
107+
var args = this.events[req].shift();
108+
req.emit.apply(req, args);
109+
}
110+
111+
// Remove the data from the event and listeners hashes
112+
delete this.listeners[req];
113+
delete this.events[req];
63114
},
64115

65-
proxyRequest: function (server, port, req, res) {
116+
proxyRequest: function (port, server, req, res) {
66117
// Remark: nodeProxy.body exists solely for testability
67118
this.body = '';
68119
var self = this;
@@ -116,20 +167,6 @@ exports.httpProxy.prototype = {
116167
reverse_proxy.end();
117168
});
118169

119-
req.removeListener('data', this.onData);
120-
req.removeListener('end', this.onEnd);
121-
122-
// Rebroadcast any events that have been buffered
123-
for (var i = 0, len = this.events.length; i < len; ++i) {
124-
req.emit.apply(req, this.events[i]);
125-
}
126-
},
127-
toArray: function (obj){
128-
var len = obj.length,
129-
arr = new Array(len);
130-
for (var i = 0; i < len; ++i) {
131-
arr[i] = obj[i];
132-
}
133-
return arr;
170+
this.unwatch(req, res);
134171
}
135172
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "http-proxy",
33
"description": "A full-featured http reverse proxy for node.js",
4-
"version": "0.1.3",
4+
"version": "0.1.4",
55
"author": "Charlie Robbins <[email protected]>",
66
"contributors": [
77
{ "name": "Marak Squires", "email": "[email protected]" }

0 commit comments

Comments
 (0)