Skip to content

Instantly share code, notes, and snippets.

@cleuton
Created April 8, 2014 15:51
Show Gist options
  • Save cleuton/10146531 to your computer and use it in GitHub Desktop.
Save cleuton/10146531 to your computer and use it in GitHub Desktop.
Naive load balancer written in Node.js
// balanceador de carga
var http = require('http');
// npm install http-proxy
var http_proxy = require('http-proxy');
var proxServer = 0;
var servidores = [
{'url':'http://localhost:8085'},
{'url':'http://localhost:8090'},
{'url':'http://localhost:8095'}];
var proxy = http_proxy.createProxy();
require('http').createServer(function(req, res) {
var servidor = servidores[proxServer];
proxy.web(req, res, {
target: servidor.url
}, function(e) { console.log(e); });
proxServer = proxServer == 2 ? 0 : proxServer + 1;
}).listen(8080);
@cleuton
Copy link
Author

cleuton commented Apr 8, 2014

It works! You have to install http-proxy ("[sudo] npm install http-proxy"). For future development, parametrize the server's table, and, perhaps, include a VM instantiation option, based on current load. You can also check the server's load before routing the request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment