[examples] Added simple load balancer example

This commit is contained in:
Marak Squires 2012-03-28 22:40:50 -07:00
parent f20b3740b1
commit fd7fcd8dec

View File

@ -0,0 +1,36 @@
var httpProxy = require('../../lib/node-http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
{
host: 'ws1.0.0.0',
port: 80
},
{
host: 'ws2.0.0.0',
port: 80
}
];
httpProxy.createServer(function (req, res, proxy) {
//
// On each request, get the first location from the list...
//
var target = addresses.shift();
//
// ...then proxy to the server whose 'turn' it is...
//
console.log('balancing request to: ', target);
proxy.proxyRequest(req, res, target);
//
// ...and then the server you just used becomes the last item in the list.
//
addresses.push(target);
}).listen(8000);
// Rinse; repeat; enjoy.