Skip to content

Commit 5d94ae2

Browse files
committed
[api] More changes for createServer api
1 parent 2e2b55f commit 5d94ae2

File tree

5 files changed

+129
-93
lines changed

5 files changed

+129
-93
lines changed

Diff for: README.md

+38-21
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,56 @@ 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
28-
29-
npm install http-proxy
30-
29+
<pre>
30+
npm install http-proxy
31+
</pre>
3132

3233
### How to use node-http-proxy
34+
<pre>
35+
var http = require('http'),
36+
httpProxy = require('http-proxy');
3337

34-
var sys = require('sys'),
35-
colors = require('colors'),
36-
http = require('http'),
37-
httpProxy = require('http-proxy').httpProxy;
38-
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);
38+
httpProxy.createServer('localhost', '9000').listen(8000);
4439

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);
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>
5046

5147
see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
48+
49+
### How to use node-http-proxy with custom server logic
50+
<pre>
51+
var http = require('http'),
52+
httpProxy = require('http-proxy');
53+
54+
httpProxy.createServer(function (req, res, proxy) {
55+
//
56+
// Put your custom server logic here
57+
//
58+
proxy.proxyRequest('localhost', '9000', req, res);
59+
}).listen(8000);
60+
61+
http.createServer(function (req, res){
62+
res.writeHead(200, {'Content-Type': 'text/plain'});
63+
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
64+
res.end();
65+
}).listen(9000);
66+
</pre>
67+
5268
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
5369

54-
if you have a suggestion for a feature currently not supported, feel free to open a [support issue](http://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
70+
If you have a suggestion for a feature currently not supported, feel free to open a [support issue](http://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.
5571

5672
<br/><br/><br/><br/><br/>
73+
5774
### License
5875

5976
(The MIT License)

Diff for: demo.js

+3-9
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('http-proxy').HttpProxy;
3131

3232
// ascii art from http://github.com/marak/asciimo
3333
var welcome = '\
@@ -40,17 +40,11 @@ var welcome = '\
4040
sys.puts(welcome.rainbow.bold);
4141

4242
// 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);
43+
httpProxy.createServer('localhost', '9000').listen(8000);
4844
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
4945

5046
// http proxy server with latency
51-
http.createServer(function (req, res){
52-
var proxy = new (httpProxy);
53-
proxy.init(req, res);
47+
httpProxy.createServer(function (req, res, proxy){
5448
setTimeout(function(){
5549
proxy.proxyRequest('localhost', '9000', req, res);
5650
}, 200)

Diff for: lib/node-http-proxy.js

+76-38
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,90 @@ 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(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 args = Array.prototype.slice.call(arguments),
55+
self = this,
56+
server,
57+
port,
58+
callback;
59+
60+
if (typeof(args[0]) === "function") {
61+
callback = args[0];
62+
}
63+
else {
64+
server = args[0];
65+
port = args[1];
66+
}
67+
68+
var proxyServer = http.createServer(function (req, res){
69+
self.watch(req, res);
70+
71+
// If we were passed a callback to process the request
72+
// or response in some way, then call it.
73+
if(callback) {
74+
callback(req, res, self);
75+
}
76+
else {
77+
self.proxyRequest(server, port, req, res);
78+
}
79+
});
80+
81+
return proxyServer;
82+
},
83+
84+
watch: function (req, res) {
5285
var self = this;
5386

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)));
87+
this.events[req] = [];
88+
89+
this.listeners[req] = {
90+
onData: function () {
91+
self.events[req].push(['data'].concat(self.toArray(arguments)));
92+
},
93+
onEnd: function () {
94+
self.events[req].push(['end'].concat(self.toArray(arguments)));
95+
}
5996
};
6097

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

65117
proxyRequest: function (server, port, req, res) {
@@ -116,20 +168,6 @@ exports.httpProxy.prototype = {
116168
reverse_proxy.end();
117169
});
118170

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;
171+
this.unwatch(req, res);
134172
}
135173
};

Diff for: 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]" }

Diff for: test/node-http-proxy-test.js

+11-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
node-http-proxy.js: http proxy for node.js
2+
node-http-proxy-test.js: http proxy for node.js
33
44
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
55
@@ -31,28 +31,14 @@ var vows = require('vows'),
3131

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

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

37-
//
38-
// Simple 'hello world' response for test purposes
39-
//
40-
var helloWorld = function(req, res) {
41-
res.writeHead(200, {'Content-Type': 'text/plain'});
42-
res.write('hello world')
43-
res.end();
44-
};
45-
4637
//
4738
// Creates the reverse proxy server
4839
//
4940
var startProxyServer = function (server, port, proxy) {
50-
var proxyServer = http.createServer(function (req, res){
51-
// Initialize the nodeProxy and start proxying the request
52-
proxy.init(req, res);
53-
proxy.proxyRequest(server, port, req, res);
54-
});
55-
41+
var proxyServer = proxy.createServer(server, port);
5642
proxyServer.listen(8080);
5743
return proxyServer;
5844
};
@@ -61,9 +47,8 @@ var startProxyServer = function (server, port, proxy) {
6147
// Creates the reverse proxy server with a specified latency
6248
//
6349
var startLatentProxyServer = function (server, port, proxy, latency) {
64-
var proxyServer = http.createServer(function (req, res){
65-
// Initialize the nodeProxy and start proxying the request
66-
proxy.init(req, res);
50+
// Initialize the nodeProxy and start proxying the request
51+
var proxyServer = proxy.createServer(function (req, res, proxy) {
6752
setTimeout(function () {
6853
proxy.proxyRequest(server, port, req, res);
6954
}, latency);
@@ -78,8 +63,10 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
7863
//
7964
var startTargetServer = function (port) {
8065
var targetServer = http.createServer(function (req, res) {
81-
helloWorld(req, res);
82-
})
66+
res.writeHead(200, {'Content-Type': 'text/plain'});
67+
res.write('hello world')
68+
res.end();
69+
});
8370

8471
targetServer.listen(port);
8572
return targetServer;
@@ -107,7 +94,7 @@ vows.describe('node-proxy').addBatch({
10794
"When an incoming request is proxied to the helloNode server" : {
10895
"with no latency" : {
10996
topic: function () {
110-
var proxy = new httpProxy;
97+
var proxy = new (HttpProxy);
11198
startTest(proxy, 8082);
11299
proxy.emitter.addListener('end', this.callback);
113100

@@ -124,7 +111,7 @@ vows.describe('node-proxy').addBatch({
124111
},
125112
"with latency": {
126113
topic: function () {
127-
var proxy = new httpProxy;
114+
var proxy = new (HttpProxy);
128115
startTestWithLatency(proxy, 8083);
129116
proxy.emitter.addListener('end', this.callback);
130117

0 commit comments

Comments
 (0)