Skip to content

Commit b1eb13e

Browse files
committed
added createServer but hated it, gonna remove
1 parent c4b7c0d commit b1eb13e

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,23 @@
1414
- written entirely in javascript
1515
- easy to use api
1616

17+
### Todo
18+
- add ability to black list ip addresses
19+
1720
### When to use node-http-proxy
1821

1922
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.
2023

2124
### Installing node-http-proxy
2225

23-
<pre>
24-
npm install http-proxy
25-
</pre>
26+
npm install http-proxy
2627

2728
### How to use node-http-proxy
29+
30+
#### usage 1:&nbsp;&nbsp;&nbsp;creating a stand-alone proxy server
31+
32+
#### usage 2:&nbsp;&nbsp;&nbsp;proxying existing http.Server requests
33+
34+
### Why doesn't node-http-proxy have more advanced features like x, y, or z?
35+
36+
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

demo.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* node-proxy-test.js: Tests for node-proxy. Reverse proxy for node.js
33
*
4-
* (C) 2010 Charlie Robbins
4+
* (C) 2010 Charlie Robbins, Marak Squires
55
* MIT LICENSE
66
*
77
*/
@@ -12,7 +12,7 @@ var vows = require('vows'),
1212
assert = require('assert'),
1313
http = require('http');
1414

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

1818

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

29+
30+
// create regular http proxy server
31+
httpProxy.createServer('localhost', 9000, function (req, res){
32+
33+
sys.puts('any requests going to 8002 will get proxied to 9000');
34+
35+
}).listen('localhost', 8002);
36+
37+
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
38+
39+
40+
2941
// create regular http proxy server
3042
http.createServer(function (req, res){
31-
var proxy = new (HttpProxy);
43+
var proxy = new httpProxy.httpProxy;
3244
proxy.init(req, res);
45+
sys.puts('proxying request to http://localhost:9000');
3346
proxy.proxyRequest('localhost', '9000', req, res);
3447
}).listen(8000);
3548
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
3649

3750
// http proxy server with latency
3851
http.createServer(function (req, res){
39-
var proxy = new (HttpProxy);
52+
var proxy = new (httpProxy);
4053
proxy.init(req, res);
4154
setTimeout(function(){
4255
proxy.proxyRequest('localhost', '9000', req, res);
@@ -47,9 +60,7 @@ sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '
4760
// create regular http server
4861
http.createServer(function (req, res){
4962
res.writeHead(200, {'Content-Type': 'text/plain'});
50-
res.write('foo');
63+
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
5164
res.end();
5265
}).listen(9000);
5366
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '9000 '.yellow);
54-
//sys.puts('to test the proxy server, request http://localhost:8080/ in your browser.');
55-
//sys.puts('your request will proxy to the server running on port 8081');

lib/node-http-proxy.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var sys = require('sys'),
1010
http = require('http'),
1111
events = require('events');
1212

13-
exports.HttpProxy = function () {
13+
exports.httpProxy = function () {
1414
var self = this;
1515
this.emitter = new(events.EventEmitter);
1616

@@ -21,16 +21,16 @@ exports.HttpProxy = function () {
2121
}
2222
};
2323

24-
exports.HttpProxy.prototype = {
25-
toArray: function (obj){
26-
var len = obj.length,
27-
arr = new Array(len);
28-
for (var i = 0; i < len; ++i) {
29-
arr[i] = obj[i];
30-
}
31-
return arr;
32-
},
33-
24+
exports.createServer = function(callback){
25+
sys.puts('httpProxy.createServer');
26+
this.listen = function(host, port){
27+
sys.puts(host + port);
28+
};
29+
return this;
30+
};
31+
32+
33+
exports.httpProxy.prototype = {
3434
init: function (req, res) {
3535
this.events = [];
3636
var self = this;
@@ -45,7 +45,7 @@ exports.HttpProxy.prototype = {
4545
req.addListener('data', this.onData);
4646
req.addListener('end', this.onEnd);
4747
},
48-
48+
4949
proxyRequest: function (server, port, req, res) {
5050
// Remark: nodeProxy.body exists solely for testability
5151
this.body = '';
@@ -107,5 +107,13 @@ exports.HttpProxy.prototype = {
107107
for (var i = 0, len = this.events.length; i < len; ++i) {
108108
req.emit.apply(req, this.events[i]);
109109
}
110+
},
111+
toArray: function (obj){
112+
var len = obj.length,
113+
arr = new Array(len);
114+
for (var i = 0; i < len; ++i) {
115+
arr[i] = obj[i];
116+
}
117+
return arr;
110118
}
111119
};

test/node-http-proxy-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var vows = require('vows'),
1313

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

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

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

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

0 commit comments

Comments
 (0)