Skip to content

Instantly share code, notes, and snippets.

@freekrai
Created October 16, 2015 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freekrai/dea154b5b3d615f3e1e4 to your computer and use it in GitHub Desktop.
Save freekrai/dea154b5b3d615f3e1e4 to your computer and use it in GitHub Desktop.
Get current subdomain in expressjs
apiapp.use(function (req, res, next) {
var domain = req.headers.host,
subDomain = domain.split('.');
if(subDomain.length > 2){
subDomain = subDomain[0].split("-").join(" ");
}else{
subDomain = "www";
}
res.locals = {
subdomain: subDomain
};
next();
});
/*
Then you can called req.locals.subdomain at anytime to get the current subdomain, useful for hosting, etc.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment