Skip to content

Instantly share code, notes, and snippets.

@michael-wolfenden
Last active October 8, 2015 01:43
Show Gist options
  • Save michael-wolfenden/c31c2c3d8eaf7b7a093b to your computer and use it in GitHub Desktop.
Save michael-wolfenden/c31c2c3d8eaf7b7a093b to your computer and use it in GitHub Desktop.
Node - Simple round robin load balancer
// 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