Created
June 21, 2011 20:51
-
-
Save gernotger/1038869 to your computer and use it in GitHub Desktop.
simple Loadbalanger for NodeJS using node-http-proxy module
This file contains 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 fs = require('fs'), | |
http = require('http'), | |
https = require('https'), | |
httpProxy = require('http-proxy'); | |
var options = { | |
https: { | |
key: fs.readFileSync('key.pem', 'utf8'), | |
cert: fs.readFileSync('cert.pem', 'utf8') | |
} | |
}; | |
var addresses = [ | |
{ | |
host: '1.2.3.4', | |
port: 3000 | |
}, | |
{ | |
host: '4.3.2.1', | |
port: 3001 | |
} | |
]; | |
// Next line is for HTTP targets | |
//------------------ | |
var proxy = new httpProxy.HttpProxy(); | |
// When you have https targets (uncomment these and comment the above line for this) | |
//------------------ | |
//var proxy = new httpProxy.HttpProxy({. | |
// target: { | |
// https: true | |
// } | |
//}); | |
https.createServer(options.https, function (req, res) { | |
var target = addresses.shift(); | |
proxy.proxyRequest(req, res, target); | |
addresses.push(target); | |
}).listen(443, "1.2.3.4"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment