From fd7fcd8decbf0c7ab00cab84e151991e380b8fae Mon Sep 17 00:00:00 2001 From: Marak Squires Date: Wed, 28 Mar 2012 22:40:50 -0700 Subject: [PATCH] [examples] Added simple load balancer example --- examples/balancer/simple-balancer.js | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/balancer/simple-balancer.js diff --git a/examples/balancer/simple-balancer.js b/examples/balancer/simple-balancer.js new file mode 100644 index 0000000..f1fa0c0 --- /dev/null +++ b/examples/balancer/simple-balancer.js @@ -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. \ No newline at end of file