diff --git a/README.md b/README.md index 6619ff6..c7c88ff 100644 --- a/README.md +++ b/README.md @@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w npm install http-proxy -### How to use node-http-proxy +### How to setup a basic proxy server
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'});
@@ -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
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);
+
+
+
+### How to proxy requests with a regular http server
+
+ 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){
diff --git a/demo.js b/demo.js
index 93b244c..6887306 100644
--- a/demo.js
+++ b/demo.js
@@ -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 );
@@ -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);
diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js
index 5b831f9..ad59c2e 100644
--- a/lib/node-http-proxy.js
+++ b/lib/node-http-proxy.js
@@ -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){
diff --git a/test/node-http-proxy-test.js b/test/node-http-proxy-test.js
index bf5e9b3..dc7037e 100644
--- a/test/node-http-proxy-test.js
+++ b/test/node-http-proxy-test.js
@@ -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;
};
@@ -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);
});
@@ -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));
};
@@ -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));
};