Last active
December 15, 2015 09:19
-
-
Save gouldingken/5237052 to your computer and use it in GitHub Desktop.
Node.js routing subdomains to folders with node-static
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 _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