Skip to content

Commit

Permalink
[examples] Added simple load balancer example
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Mar 29, 2012
1 parent f20b374 commit fd7fcd8
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/balancer/simple-balancer.js
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit fd7fcd8

Please sign in to comment.