Skip to content

Instantly share code, notes, and snippets.

@gorork
Created November 24, 2016 08:03
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 gorork/f6e170c7c671f76ac410ea3adf266b4b to your computer and use it in GitHub Desktop.
Save gorork/f6e170c7c671f76ac410ea3adf266b4b to your computer and use it in GitHub Desktop.
How to reate a sub domain for each user
/**
* 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