Created
August 25, 2014 21:55
-
-
Save vkarpov15/c58b1cabb5250e4c999c to your computer and use it in GitHub Desktop.
15 line (but not durable) HTTP load balancer with NodeJS
This file contains hidden or 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
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