forked from CSC-DevOps/Queues
-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.js
38 lines (35 loc) · 915 Bytes
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Skeleton Code From
// https://github.com/nodejitsu/node-http-proxy/blob/master/examples/balancer/simple-balancer-with-websockets.js
var http = require('http'),
httpProxy = require('http-proxy');
//
// A simple round-robin load balancing strategy.
//
// First, list the servers you want to use in your rotation.
//
var addresses = [
{
host: 'localhost',
port: 3001
},
{
host: 'localhost',
port: 3002
}
];
var proxy = httpProxy.createServer();
http.createServer(function (req, res) {
//
// On each request, get the first location from the list...
//
var target = { target: addresses.shift() };
//
// ...then proxy to the server whose 'turn' it is...
//
console.log('balancing request to: ', target);
proxy.web(req, res, target);
//
// ...and then the server you just used becomes the last item in the list.
//
addresses.push(target.target);
}).listen(80);