Skip to content

Instantly share code, notes, and snippets.

@gouldingken
Last active December 15, 2015 09:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gouldingken/5237052 to your computer and use it in GitHub Desktop.
Save gouldingken/5237052 to your computer and use it in GitHub Desktop.
Node.js routing subdomains to folders with node-static
var _subServants = {};
var _addSubdomainDir = function (subdomain, relativePath) {
var cliPath = path.resolve(__dirname, relativePath);
_subServants[subdomain] = new (nodeStatic.Server)(cliPath);
};
var _init = function () {
_addSubdomainDir('brown', 'client/branches/brown');
_addSubdomainDir('green', 'client/branches/green');
_addSubdomainDir('pink', 'client/branches/pink');
};
this.serve = function (req, res) {
var subdomain = req.headers.host.split('.')[0];
if (_subServants[subdomain]) {
_subServants[subdomain].serve(req, res);
return;
}
res.writeHeader(200, {"Content-Type": "application/json"});
res.write(JSON.stringify({message: 'root server serving'}));
res.end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment