Last active
October 8, 2015 01:43
-
-
Save michael-wolfenden/c31c2c3d8eaf7b7a093b to your computer and use it in GitHub Desktop.
Node - Simple round robin load balancer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cmd> node index.js | |
// requests to port 8080 will round robin between ports 8081 & 8082 | |
var http = require('http'); | |
var httpProxy = require('http-proxy'); | |
var proxy = httpProxy.createProxyServer(); | |
var servers = [ | |
'http://127.0.0.1:8081', | |
'http://127.0.0.1:8082' | |
]; | |
http.createServer(function (req, res) { | |
if (req.url === '/favicon.ico') return; | |
var target = servers.shift(); | |
console.log('proxying traffic to :%s', target); | |
proxy.web(req, res, { | |
target: target | |
}); | |
servers.push(target); | |
}).listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment