Skip to content

Instantly share code, notes, and snippets.

@vkarpov15
Created August 25, 2014 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkarpov15/c58b1cabb5250e4c999c to your computer and use it in GitHub Desktop.
Save vkarpov15/c58b1cabb5250e4c999c to your computer and use it in GitHub Desktop.
15 line (but not durable) HTTP load balancer with NodeJS
var http = require('http');
var request = require('request');
var addrs = [
'http://127.0.0.1:3000',
'http://127.0.0.1:3001'
];
var index = 0;
http.createServer(function(req, res) {
var proxied = request({ url: addrs[index] + req.url, followRedirect: false });
req.pipe(proxied);
proxied.pipe(res);
index = (index + 1) % 2;
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment