Created
November 24, 2016 08:03
-
-
Save gorork/f6e170c7c671f76ac410ea3adf266b4b to your computer and use it in GitHub Desktop.
How to reate a sub domain for each user
This file contains hidden or 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
/** | |
* dynamic A record | |
* eg. *.domain.com points to domain.com? | |
* then in your router | |
* | |
* source: https://github.com/bmullan91/express-subdomain/issues/26#issuecomment-225441455 | |
*/ | |
// get your dynamic subdomain | |
app.use(function(req, res, next) { | |
if (!req.subdomains.length || req.subdomains.slice(-1)[0] === 'www') return next(); | |
// otherwise we have subdomain here | |
var subdomain = req.subdomains.slice(-1)[0]; | |
// keep it | |
req.subdomain = subdomain; | |
next(); | |
}); | |
// render a page | |
app.get('/', function(req, res) { | |
// no subdomain | |
if (!req.subdomain) { | |
// render landing page | |
res.render('home'); | |
} else { | |
// render subdomain specific data | |
res.render('user-page', { subdomain: req.subdomain }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment