mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
changed api to better reflect nodes api. updated demos, tests, docs
This commit is contained in:
parent
fb8c5abd3c
commit
bde98f4892
24
README.md
24
README.md
@ -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'});
|
||||
@ -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){
|
||||
|
||||
6
demo.js
6
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);
|
||||
|
||||
@ -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){
|
||||
|
||||
@ -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));
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user