app.all('*', (req, res) => {
// Find out if we are requesting the subdomain
if ( ( req.subdomains || [false] )[0] === 'sub' ) {
// If we are, and the path is /resetpassword, then add resetpw=true to the URL params (flattening the preexisting ones into a new string)
if ( req.path === 'resetpassword' ) {
res.location('https://sub.domain.com/' + '?resetpw=true' + Object.keys(req.query).map(k => k + '=' + req.query[k]).join('&'));
}
// Then serve static files from the sub directory
app.use(express.static(__dirname + 'public/sub', { extensions: ['html', 'htm'] }));
} else {
// Otherwise, serve static files from the main directory
app.use(express.static(__dirname + 'public/main', { extensions: ['html', 'htm'] }));
}
});const baseUrl = '/bundle/cloud/public/',
vhost = require('vhost'),
express = require('express'),
routerMain = express.Router(),
routerSub = express.Router(),
staticOpts = { extensions: ['html', 'htm'] };
//sub.domain.com
routerSub.use((req, res, next) => {
// subdomain-specific logic
next();
});
routerSub.use(express.static(baseUrl + 'sub/', staticOpts));
// domain.com
routerMain.get('/sub', (req, res) => {
res.redirect('https://sub.domain.com/');
});
routerMain.use(express.static(baseUrl, staticOpts));
// Connect routers
app.use(vhost('sub.domain.com', routerSub));
app.use(routerMain);