Skip to content

Instantly share code, notes, and snippets.

@nodesocket
Created December 4, 2011 07:36
Show Gist options
  • Save nodesocket/1429527 to your computer and use it in GitHub Desktop.
Save nodesocket/1429527 to your computer and use it in GitHub Desktop.
http-proxy re-write
var gear;
var starts_with_slash;
//Get if the request starts with a slash
req.url.substring(0, 1) === '/' ? starts_with_slash = true : starts_with_slash = false;
//Request starts with slash
if(starts_with_slash) {
//The position of the second slash, since the first slash is at index 0
var end = req.url.indexOf('/', 1);
end === -1 ? gear = req.url.substring(1) : gear = req.url.substring(1, end);
}
//Request does not start with a slash
else {
//The position of the first slash
var end = req.url.indexOf('/', 0);
end === -1 ? gear = req.url.substring(0) : gear = req.url.substring(0, end);
}
//O(1) lookup in hash routes
var route = routes[gear];
if(typeof route === "object") {
//Rewrite magic for express
if(starts_with_slash) {
req.url = req.url.replace('/' + route.gear, '');
} else {
req.url = req.url.replace(route.gear, '');
}
proxy.proxyRequest(req, res, {
host: route.host,
port: route.port
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment