Commit bde98f48 authored by Marak Squires's avatar Marak Squires

changed api to better reflect nodes api. updated demos, tests, docs

parent fb8c5abd
...@@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w ...@@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w
npm install http-proxy npm install http-proxy
</pre> </pre>
### How to use node-http-proxy ### How to setup a basic proxy server
<pre> <pre>
var http = require('http'), var http = require('http'),
httpProxy = require('http-proxy'); httpProxy = require('http-proxy');
httpProxy.createServer('localhost', '9000').listen(8000); httpProxy.createServer('9000', 'localhost').listen(8000);
http.createServer(function (req, res){ http.createServer(function (req, res){
res.writeHead(200, {'Content-Type': 'text/plain'}); res.writeHead(200, {'Content-Type': 'text/plain'});
...@@ -46,24 +46,36 @@ Let's suppose you were running multiple http application servers, but you only w ...@@ -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. 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> <pre>
var http = require('http'), var http = require('http'),
httpProxy = require('http-proxy'); httpProxy = require('http-proxy');
// create a proxy server with custom application logic // create a proxy server with custom application logic
httpProxy.createServer(function (req, res, proxy) { httpProxy.createServer(function (req, res, proxy) {
// Put your custom server logic here // Put your custom server logic here
proxy.proxyRequest('localhost', '9000', req, res); proxy.proxyRequest('9000', 'localhost', req, res);
}).listen(8000); }).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 // create a regular http server and proxy its handler
http.createServer(function (req, res){ http.createServer(function (req, res){
var proxy = new httpProxy.HttpProxy; var proxy = new httpProxy.HttpProxy;
proxy.watch(req, res); proxy.watch(req, res);
// Put your custom server logic here // Put your custom server logic here
proxy.proxyRequest('localhost', 9000, req, res); proxy.proxyRequest(9000, 'localhost', req, res);
}).listen(8001); }).listen(8001);
http.createServer(function (req, res){ http.createServer(function (req, res){
......
...@@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold); ...@@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold);
/****** basic http proxy server ******/ /****** 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); sys.puts('http proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8000'.yellow);
/****** http proxy server with latency******/ /****** http proxy server with latency******/
httpProxy.createServer(function (req, res, proxy){ httpProxy.createServer(function (req, res, proxy){
setTimeout(function(){ setTimeout(function(){
proxy.proxyRequest('localhost', 9000, req, res); proxy.proxyRequest(9000, 'localhost', req, res);
}, 200) }, 200)
}).listen(8001); }).listen(8001);
sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline ); sys.puts('http proxy server '.blue + 'started '.green.bold + 'on port '.blue + '8001 '.yellow + 'with latency'.magenta.underline );
...@@ -58,7 +58,7 @@ http.createServer(function (req, res){ ...@@ -58,7 +58,7 @@ http.createServer(function (req, res){
proxy.watch(req, res); proxy.watch(req, res);
setTimeout(function(){ setTimeout(function(){
proxy.proxyRequest('localhost', 9000, req, res); proxy.proxyRequest(9000, 'localhost', req, res);
}, 200); }, 200);
}).listen(8002); }).listen(8002);
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta); sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);
......
...@@ -61,8 +61,8 @@ exports.HttpProxy.prototype = { ...@@ -61,8 +61,8 @@ exports.HttpProxy.prototype = {
callback = arguments[0]; callback = arguments[0];
} }
else { else {
server = arguments[0]; port = arguments[0];
port = arguments[1]; server = arguments[1];
} }
var proxyServer = http.createServer(function (req, res){ var proxyServer = http.createServer(function (req, res){
......
...@@ -37,8 +37,8 @@ var testServers = {}; ...@@ -37,8 +37,8 @@ var testServers = {};
// //
// Creates the reverse proxy server // Creates the reverse proxy server
// //
var startProxyServer = function (server, port, proxy) { var startProxyServer = function (port, server, proxy) {
var proxyServer = proxy.createServer(server, port); var proxyServer = proxy.createServer(port, server);
proxyServer.listen(8080); proxyServer.listen(8080);
return proxyServer; return proxyServer;
}; };
...@@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) { ...@@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
// Initialize the nodeProxy and start proxying the request // Initialize the nodeProxy and start proxying the request
var proxyServer = proxy.createServer(function (req, res, proxy) { var proxyServer = proxy.createServer(function (req, res, proxy) {
setTimeout(function () { setTimeout(function () {
proxy.proxyRequest(server, port, req, res); proxy.proxyRequest(port, server, req, res);
}, latency); }, latency);
}); });
...@@ -77,7 +77,7 @@ var startTargetServer = function (port) { ...@@ -77,7 +77,7 @@ var startTargetServer = function (port) {
// //
var startTest = function (proxy, port) { var startTest = function (proxy, port) {
testServers.noLatency = []; testServers.noLatency = [];
testServers.noLatency.push(startProxyServer('localhost', port, proxy)); testServers.noLatency.push(startProxyServer(port, 'localhost', proxy));
testServers.noLatency.push(startTargetServer(port)); testServers.noLatency.push(startTargetServer(port));
}; };
...@@ -86,7 +86,7 @@ var startTest = function (proxy, port) { ...@@ -86,7 +86,7 @@ var startTest = function (proxy, port) {
// //
var startTestWithLatency = function (proxy, port) { var startTestWithLatency = function (proxy, port) {
testServers.latency = []; testServers.latency = [];
testServers.latency.push(startLatentProxyServer('localhost', port, proxy, 2000)); testServers.latency.push(startLatentProxyServer(port, 'localhost', proxy, 2000));
testServers.latency.push(startTargetServer(port)); testServers.latency.push(startTargetServer(port));
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment