-
Notifications
You must be signed in to change notification settings - Fork 8
Scraps
tracend edited this page Apr 1, 2013
·
1 revision
Graveyard of old code:
// main proxy logic
httpProxy.createServer(function (req, res, proxy) {
//
// Proxy both HTTP & HTTPS requests
//
//
// Put your custom server logic here
//
// Example:
// A simple round-robin load balancing strategy.
//
// if (ips instanceof Array) {
// var target = loadBalance(ips);
// proxy.proxyRequest(req, res, target);
//
//} else {
//}
//
// check if this is an express server
var domains = config.hosts.express;
var routes = config.routes;
var host = req.headers.host || false;
var protocol = req.socket.encrypted ? 'https' : 'http';
// #4 - Always redirect a www request
if (/^www/.test(host)){
// remove www prefix
host = host.replace(/^www\./, '');
//
res.writeHead(301,
{Location: protocol+ '://'+host + req.url });
res.end();
}
//#14 FIX: removing port from host
if(host.indexOf(":") > -1) host = host.substring(0, host.indexOf(":") );
// get the 'naked' domain
var domain = host;
if (/^dev/.test(domain)){
// include www here?
domain = domain.replace(/^dev\./, '');
}
// #17 - check direct routes
// pick the port
var port = ((domains.indexOf(domain) > -1) ? config.ports.express : config.ports.router) || false;
// don't continue if there is no host/port
if( !host || !port ) return;
proxy.proxyRequest(req, res, {
host: host,
port: port
});
}).listen(config.ports.proxy);