Created
October 16, 2015 15:40
-
-
Save freekrai/dea154b5b3d615f3e1e4 to your computer and use it in GitHub Desktop.
Get current subdomain in expressjs
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
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