Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.