Skip to content

Commit fd7fcd8

Browse files
committed
[examples] Added simple load balancer example
1 parent f20b374 commit fd7fcd8

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

examples/balancer/simple-balancer.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var httpProxy = require('../../lib/node-http-proxy');
2+
//
3+
// A simple round-robin load balancing strategy.
4+
//
5+
// First, list the servers you want to use in your rotation.
6+
//
7+
var addresses = [
8+
{
9+
host: 'ws1.0.0.0',
10+
port: 80
11+
},
12+
{
13+
host: 'ws2.0.0.0',
14+
port: 80
15+
}
16+
];
17+
18+
httpProxy.createServer(function (req, res, proxy) {
19+
//
20+
// On each request, get the first location from the list...
21+
//
22+
var target = addresses.shift();
23+
24+
//
25+
// ...then proxy to the server whose 'turn' it is...
26+
//
27+
console.log('balancing request to: ', target);
28+
proxy.proxyRequest(req, res, target);
29+
30+
//
31+
// ...and then the server you just used becomes the last item in the list.
32+
//
33+
addresses.push(target);
34+
}).listen(8000);
35+
36+
// Rinse; repeat; enjoy.

0 commit comments

Comments
 (0)