Skip to content

Commit a9084b9

Browse files
committed
[api test dist doc] Updated for 0.3.0 release
1 parent 9f0aeac commit a9084b9

File tree

8 files changed

+105
-125
lines changed

8 files changed

+105
-125
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
node-http-proxy
33

4-
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/node-http-proxy
4+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires
55

66
Permission is hereby granted, free of charge, to any person obtaining
77
a copy of this software and associated documentation files (the

README.md

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# node-http-proxy - v0.1.5
1+
# node-http-proxy - v0.3.0
22

33
<img src = "http://i.imgur.com/dSSUX.png"/>
44

@@ -19,7 +19,6 @@
1919

2020
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.
2121

22-
2322
### Installing npm (node package manager)
2423
<pre>
2524
curl http://npmjs.org/install.sh | sh
@@ -44,7 +43,7 @@ Let's suppose you were running multiple http application servers, but you only w
4443
}).listen(9000);
4544
</pre>
4645

47-
see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
46+
See the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js) for further examples.
4847

4948
### How to setup a proxy server with custom server logic
5049
<pre>
@@ -54,15 +53,34 @@ see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
5453
// create a proxy server with custom application logic
5554
httpProxy.createServer(function (req, res, proxy) {
5655
// Put your custom server logic here
57-
proxy.proxyRequest(9000, 'localhost', req, res);
56+
proxy.proxyRequest(9000, 'localhost');
57+
}).listen(8000);
58+
59+
http.createServer(function (req, res){
60+
res.writeHead(200, {'Content-Type': 'text/plain'});
61+
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
62+
res.end();
63+
}).listen(9000);
64+
</pre>
65+
66+
### How to setup a proxy server with latency (e.g. IO, etc)
67+
<pre>
68+
var http = require('http'),
69+
httpProxy = require('http-proxy');
70+
71+
// create a proxy server with custom application logic
72+
httpProxy.createServer(function (req, res, proxy) {
73+
// Wait for two seconds then respond
74+
setTimeout(function () {
75+
proxy.proxyRequest(9000, 'localhost');
76+
}, 2000);
5877
}).listen(8000);
5978

6079
http.createServer(function (req, res){
6180
res.writeHead(200, {'Content-Type': 'text/plain'});
6281
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
6382
res.end();
6483
}).listen(9000);
65-
6684
</pre>
6785

6886
### How to proxy requests with a regular http server
@@ -72,30 +90,34 @@ see the [demo](http://github.com/nodejitsu/node-http-proxy/blob/master/demo.js)
7290

7391
// create a regular http server and proxy its handler
7492
http.createServer(function (req, res){
75-
var proxy = new httpProxy.HttpProxy;
76-
proxy.watch(req, res);
77-
// Put your custom server logic here
93+
// Create a new instance of HttProxy for this request
94+
// each instance is only valid for serving one request
95+
//
96+
// Don't worry benchmarks show the object
97+
// creation is lightning fast
98+
var proxy = new httpProxy.HttpProxy(req, res);
99+
100+
// Put your custom server logic here, then proxy
78101
proxy.proxyRequest(9000, 'localhost', req, res);
79102
}).listen(8001);
80103

81104
http.createServer(function (req, res){
82105
res.writeHead(200, {'Content-Type': 'text/plain'});
83106
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
84107
res.end();
85-
}).listen(9000);
86-
108+
}).listen(9000);
87109
</pre>
88110

89111
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
90112

91113
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.
92114

93-
<br/><hr/>
115+
<br/>
94116
### License
95117

96118
(The MIT License)
97119

98-
Copyright (c) 2010 Charlie Robbins & Marak Squires http://github.com/nodejitsu/
120+
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, & Marak Squires
99121

100122
Permission is hereby granted, free of charge, to any person obtaining
101123
a copy of this software and associated documentation files (the
@@ -116,4 +138,4 @@ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
116138
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
117139
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
118140

119-
[0]:http://nodejitsu.com "nodejitsu.com"
141+
[0]: http://nodejitsu.com

benchmarks/http-server-base

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[Marak@Maraks-MacBook-Pro ~/dev/zalgo.js]$ ab -c 50 -n 1000 -k http://127.0.0.1:9000/
1+
$ ab -c 50 -n 1000 -k http://127.0.0.1:9000/
22
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
33
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
44
Licensed to The Apache Software Foundation, http://www.apache.org/

benchmarks/proxy-to-http

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[Marak@Maraks-MacBook-Pro ~]$ ab -c 5 -n 1000 http://127.0.0.1:8000/
1+
$ ab -c 5 -n 1000 http://127.0.0.1:8000/
22
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
33
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
44
Licensed to The Apache Software Foundation, http://www.apache.org/
@@ -22,35 +22,35 @@ Server Hostname: 127.0.0.1
2222
Server Port: 8000
2323

2424
Document Path: /
25-
Document Length: 155 bytes
25+
Document Length: 160 bytes
2626

2727
Concurrency Level: 5
28-
Time taken for tests: 0.602 seconds
28+
Time taken for tests: 0.450 seconds
2929
Complete requests: 1000
30-
Failed requests: 5
31-
(Connect: 0, Receive: 0, Length: 5, Exceptions: 0)
30+
Failed requests: 1
31+
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
3232
Write errors: 0
33-
Total transferred: 247030 bytes
34-
HTML transferred: 155030 bytes
35-
Requests per second: 1660.64 [#/sec] (mean)
36-
Time per request: 3.011 [ms] (mean)
37-
Time per request: 0.602 [ms] (mean, across all concurrent requests)
38-
Transfer rate: 400.61 [Kbytes/sec] received
33+
Total transferred: 252006 bytes
34+
HTML transferred: 160006 bytes
35+
Requests per second: 2219.97 [#/sec] (mean)
36+
Time per request: 2.252 [ms] (mean)
37+
Time per request: 0.450 [ms] (mean, across all concurrent requests)
38+
Transfer rate: 546.33 [Kbytes/sec] received
3939

4040
Connection Times (ms)
4141
min mean[+/-sd] median max
42-
Connect: 0 0 0.1 0 1
43-
Processing: 2 3 0.8 3 10
44-
Waiting: 2 3 0.8 3 10
45-
Total: 2 3 0.8 3 10
42+
Connect: 0 0 0.0 0 0
43+
Processing: 1 2 0.9 2 10
44+
Waiting: 1 2 0.9 2 10
45+
Total: 1 2 0.9 2 10
4646

4747
Percentage of the requests served within a certain time (ms)
48-
50% 3
49-
66% 3
48+
50% 2
49+
66% 2
5050
75% 3
5151
80% 3
5252
90% 3
53-
95% 4
54-
98% 5
55-
99% 9
56-
100% 10 (longest request)
53+
95% 3
54+
98% 3
55+
99% 7
56+
100% 10 (longest request)

demo.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,22 @@ httpProxy.createServer(9000, 'localhost').listen(8000);
4545
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
4646

4747
/****** http proxy server with latency******/
48-
/*httpProxy.createServer(function (req, res, proxy){
48+
httpProxy.createServer(function (req, res, proxy){
4949
setTimeout(function(){
50-
proxy.proxyRequest(9000, 'localhost', req, res);
50+
proxy.proxyRequest(9000, 'localhost');
5151
}, 200)
5252
}).listen(8001);
53-
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );*/
53+
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );
5454

5555
/****** http server with proxyRequest handler and latency******/
56-
/*http.createServer(function (req, res){
57-
var proxy = new httpProxy.HttpProxy;
58-
proxy.watch(req, res);
56+
http.createServer(function (req, res){
57+
var proxy = new httpProxy.HttpProxy(req, res);
5958

6059
setTimeout(function(){
61-
proxy.proxyRequest(9000, 'localhost', req, res);
60+
proxy.proxyRequest(9000, 'localhost');
6261
}, 200);
6362
}).listen(8002);
64-
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);*/
63+
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
6564

6665
/****** regular http server ******/
6766
http.createServer(function (req, res){

lib/node-http-proxy.js

+8-59
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
var sys = require('sys'),
2828
http = require('http'),
29-
eyes = require('eyes'),
30-
pool = require('pool'),
3129
events = require('events'),
3230
pool = require('pool'),
3331
min = 0,
@@ -48,6 +46,10 @@ exports.createServer = function () {
4846
var server = http.createServer(function (req, res){
4947
var proxy = new HttpProxy(req, res);
5048

49+
proxy.emitter.on('proxy', function (err, body) {
50+
server.emit('proxy', err, body);
51+
});
52+
5153
// If we were passed a callback to process the request
5254
// or response in some way, then call it.
5355
if(callback) {
@@ -110,7 +112,7 @@ HttpProxy.prototype = {
110112

111113
// Rebroadcast any events that have been buffered
112114
for (var i = 0, len = this.events.length; i < len; ++i) {
113-
req.emit.apply(req, this.events[i]);
115+
req.emit.apply(req, this.events[i]);
114116
}
115117
},
116118

@@ -133,6 +135,7 @@ HttpProxy.prototype = {
133135

134136
res.end();
135137
};
138+
136139
// Add a listener for the connection timeout event
137140
reverse_proxy.connection.addListener('error', error);
138141

@@ -157,8 +160,7 @@ HttpProxy.prototype = {
157160
// Add event listener for end of proxied response
158161
response.addListener('end', function () {
159162
// Remark: Emit the end event for testability
160-
self.emitter.emit('end', null, self.body);
161-
163+
self.emitter.emit('proxy', null, self.body);
162164
res.end();
163165
});
164166
});
@@ -179,57 +181,4 @@ HttpProxy.prototype = {
179181
}
180182
};
181183

182-
exports.HttpProxy = HttpProxy;
183-
184-
/*// Create an error handler so we can use it temporarily
185-
var error = function (err) {
186-
res.writeHead(200, {'Content-Type': 'text/plain'});
187-
188-
if(req.method !== 'HEAD') {
189-
res.write('An error has occurred: ' + sys.puts(JSON.stringify(err)));
190-
}
191-
192-
res.end();
193-
};
194-
// Add a listener for the connection timeout event
195-
reverse_proxy.connection.addListener('error', error);
196-
197-
// Add a listener for the reverse_proxy response event
198-
reverse_proxy.addListener('response', function (response) {
199-
if (response.headers.connection) {
200-
if (req.headers.connection) response.headers.connection = req.headers.connection;
201-
else response.headers.connection = 'close';
202-
}
203-
204-
// Set the response headers of the client response
205-
res.writeHead(response.statusCode, response.headers);
206-
207-
// Add event handler for the proxied response in chunks
208-
response.addListener('data', function (chunk) {
209-
if(req.method !== 'HEAD') {
210-
res.write(chunk, 'binary');
211-
self.body += chunk;
212-
}
213-
});
214-
215-
// Add event listener for end of proxied response
216-
response.addListener('end', function () {
217-
// Remark: Emit the end event for testability
218-
self.emitter.emit('end', null, self.body);
219-
220-
res.end();
221-
});
222-
});
223-
224-
// Chunk the client request body as chunks from the proxied request come in
225-
req.addListener('data', function (chunk) {
226-
reverse_proxy.write(chunk, 'binary');
227-
})
228-
229-
// At the end of the client request, we are going to stop the proxied request
230-
req.addListener('end', function () {
231-
reverse_proxy.end();
232-
//reverse_proxy.connection.removeListener('error', error);
233-
});
234-
235-
self.unwatch(req);*/
184+
exports.HttpProxy = HttpProxy;

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "http-proxy",
33
"description": "A full-featured http reverse proxy for node.js",
4-
"version": "0.1.6",
4+
"version": "0.3.0",
55
"author": "Charlie Robbins <[email protected]>",
6-
"contributors": [
6+
"contributors": [
7+
{ "name": "Mikeal Rogers", "email": '[email protected]' },
78
{ "name": "Marak Squires", "email": "[email protected]" }
89
],
910
"repository": {
@@ -12,9 +13,10 @@
1213
},
1314
"keywords": ["reverse", "proxy", "http"],
1415
"dependencies": {
15-
"colors": ">= 0.3.0"
16+
"colors": ">= 0.3.0",
17+
"pool": ">= 0.4.1"
1618
},
1719
"main": "./lib/node-http-proxy",
1820
"scripts": { "test": "vows" },
19-
"engines": { "node": ">= 0.1.98" }
21+
"engines": { "node": ">= 0.2.0" }
2022
}

0 commit comments

Comments
 (0)