Skip to content

Commit

Permalink
changed api to better reflect nodes api. updated demos, tests, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Aug 3, 2010
1 parent fb8c5ab commit bde98f4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w
npm install http-proxy
</pre>

### How to use node-http-proxy
### How to setup a basic proxy server
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

httpProxy.createServer('localhost', '9000').listen(8000);
httpProxy.createServer('9000', 'localhost').listen(8000);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
Expand All @@ -46,24 +46,36 @@ Let's suppose you were running multiple http application servers, but you only w

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

### How to use node-http-proxy with custom server logic
### How to setup a proxy server with custom server logic
<pre>
var http = require('http'),
httpProxy = require('http-proxy');


// create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) {
// Put your custom server logic here
proxy.proxyRequest('localhost', '9000', req, res);
proxy.proxyRequest('9000', 'localhost', req, res);
}).listen(8000);

http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);

</pre>

### How to proxy requests with a regular http server
<pre>
var http = require('http'),
httpProxy = require('http-proxy');

// create a regular http server and proxy its handler
http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res);
// Put your custom server logic here
proxy.proxyRequest('localhost', 9000, req, res);
proxy.proxyRequest(9000, 'localhost', req, res);
}).listen(8001);

http.createServer(function (req, res){
Expand Down
6 changes: 3 additions & 3 deletions demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold);


/****** basic http proxy server ******/
httpProxy.createServer('localhost', 9000).listen(8000);
httpProxy.createServer(9000, 'localhost').listen(8000);
sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);

/****** http proxy server with latency******/
httpProxy.createServer(function (req, res, proxy){
setTimeout(function(){
proxy.proxyRequest('localhost', 9000, req, res);
proxy.proxyRequest(9000, 'localhost', req, res);
}, 200)
}).listen(8001);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );
Expand All @@ -58,7 +58,7 @@ http.createServer(function (req, res){
proxy.watch(req, res);

setTimeout(function(){
proxy.proxyRequest('localhost', 9000, req, res);
proxy.proxyRequest(9000, 'localhost', req, res);
}, 200);
}).listen(8002);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
Expand Down
4 changes: 2 additions & 2 deletions lib/node-http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ exports.HttpProxy.prototype = {
callback = arguments[0];
}
else {
server = arguments[0];
port = arguments[1];
port = arguments[0];
server = arguments[1];
}

var proxyServer = http.createServer(function (req, res){
Expand Down
10 changes: 5 additions & 5 deletions test/node-http-proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var testServers = {};
//
// Creates the reverse proxy server
//
var startProxyServer = function (server, port, proxy) {
var proxyServer = proxy.createServer(server, port);
var startProxyServer = function (port, server, proxy) {
var proxyServer = proxy.createServer(port, server);
proxyServer.listen(8080);
return proxyServer;
};
Expand All @@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
// Initialize the nodeProxy and start proxying the request
var proxyServer = proxy.createServer(function (req, res, proxy) {
setTimeout(function () {
proxy.proxyRequest(server, port, req, res);
proxy.proxyRequest(port, server, req, res);
}, latency);
});

Expand All @@ -77,7 +77,7 @@ var startTargetServer = function (port) {
//
var startTest = function (proxy, port) {
testServers.noLatency = [];
testServers.noLatency.push(startProxyServer('localhost', port, proxy));
testServers.noLatency.push(startProxyServer(port, 'localhost', proxy));
testServers.noLatency.push(startTargetServer(port));
};

Expand All @@ -86,7 +86,7 @@ var startTest = function (proxy, port) {
//
var startTestWithLatency = function (proxy, port) {
testServers.latency = [];
testServers.latency.push(startLatentProxyServer('localhost', port, proxy, 2000));
testServers.latency.push(startLatentProxyServer(port, 'localhost', proxy, 2000));
testServers.latency.push(startTargetServer(port));
};

Expand Down

0 comments on commit bde98f4

Please sign in to comment.