Skip to content

Commit bde98f4

Browse files
committed
changed api to better reflect nodes api. updated demos, tests, docs
1 parent fb8c5ab commit bde98f4

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

Diff for: README.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ Let's suppose you were running multiple http application servers, but you only w
3030
npm install http-proxy
3131
</pre>
3232

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

38-
httpProxy.createServer('localhost', '9000').listen(8000);
38+
httpProxy.createServer('9000', 'localhost').listen(8000);
3939

4040
http.createServer(function (req, res){
4141
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
4646

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

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

54-
5554
// create a proxy server with custom application logic
5655
httpProxy.createServer(function (req, res, proxy) {
5756
// Put your custom server logic here
58-
proxy.proxyRequest('localhost', '9000', req, res);
57+
proxy.proxyRequest('9000', 'localhost', req, res);
5958
}).listen(8000);
6059

60+
http.createServer(function (req, res){
61+
res.writeHead(200, {'Content-Type': 'text/plain'});
62+
res.write('request successfully proxied: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2));
63+
res.end();
64+
}).listen(9000);
65+
66+
</pre>
67+
68+
### How to proxy requests with a regular http server
69+
<pre>
70+
var http = require('http'),
71+
httpProxy = require('http-proxy');
72+
6173
// create a regular http server and proxy its handler
6274
http.createServer(function (req, res){
6375
var proxy = new httpProxy.HttpProxy;
6476
proxy.watch(req, res);
6577
// Put your custom server logic here
66-
proxy.proxyRequest('localhost', 9000, req, res);
78+
proxy.proxyRequest(9000, 'localhost', req, res);
6779
}).listen(8001);
6880

6981
http.createServer(function (req, res){

Diff for: demo.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ sys.puts(welcome.rainbow.bold);
4141

4242

4343
/****** basic http proxy server ******/
44-
httpProxy.createServer('localhost', 9000).listen(8000);
44+
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******/
4848
httpProxy.createServer(function (req, res, proxy){
4949
setTimeout(function(){
50-
proxy.proxyRequest('localhost', 9000, req, res);
50+
proxy.proxyRequest(9000, 'localhost', req, res);
5151
}, 200)
5252
}).listen(8001);
5353
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){
5858
proxy.watch(req, res);
5959

6060
setTimeout(function(){
61-
proxy.proxyRequest('localhost', 9000, req, res);
61+
proxy.proxyRequest(9000, 'localhost', req, res);
6262
}, 200);
6363
}).listen(8002);
6464
sys.puts('http server '.blue + 'started '.green.bold + 'on port '.blue + '8002 '.yellow + 'with proxyRequest handler'.cyan.underline + ' and latency'.magenta);

Diff for: lib/node-http-proxy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ exports.HttpProxy.prototype = {
6161
callback = arguments[0];
6262
}
6363
else {
64-
server = arguments[0];
65-
port = arguments[1];
64+
port = arguments[0];
65+
server = arguments[1];
6666
}
6767

6868
var proxyServer = http.createServer(function (req, res){

Diff for: test/node-http-proxy-test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ var testServers = {};
3737
//
3838
// Creates the reverse proxy server
3939
//
40-
var startProxyServer = function (server, port, proxy) {
41-
var proxyServer = proxy.createServer(server, port);
40+
var startProxyServer = function (port, server, proxy) {
41+
var proxyServer = proxy.createServer(port, server);
4242
proxyServer.listen(8080);
4343
return proxyServer;
4444
};
@@ -50,7 +50,7 @@ var startLatentProxyServer = function (server, port, proxy, latency) {
5050
// Initialize the nodeProxy and start proxying the request
5151
var proxyServer = proxy.createServer(function (req, res, proxy) {
5252
setTimeout(function () {
53-
proxy.proxyRequest(server, port, req, res);
53+
proxy.proxyRequest(port, server, req, res);
5454
}, latency);
5555
});
5656

@@ -77,7 +77,7 @@ var startTargetServer = function (port) {
7777
//
7878
var startTest = function (proxy, port) {
7979
testServers.noLatency = [];
80-
testServers.noLatency.push(startProxyServer('localhost', port, proxy));
80+
testServers.noLatency.push(startProxyServer(port, 'localhost', proxy));
8181
testServers.noLatency.push(startTargetServer(port));
8282
};
8383

@@ -86,7 +86,7 @@ var startTest = function (proxy, port) {
8686
//
8787
var startTestWithLatency = function (proxy, port) {
8888
testServers.latency = [];
89-
testServers.latency.push(startLatentProxyServer('localhost', port, proxy, 2000));
89+
testServers.latency.push(startLatentProxyServer(port, 'localhost', proxy, 2000));
9090
testServers.latency.push(startTargetServer(port));
9191
};
9292

0 commit comments

Comments
 (0)