Instead of suggesting changes to my application architecture, I am seeking practical solutions for my specific requirements. I have code that serves different static files based on the domain name, allowing me to run multiple static HTML sites from the same routes:
app.get('/', (req, res) => {
console.log('req.hostname: ', req.hostname)
if (req.hostname === 'localhost') {
app.use(express.static(path.join(__dirname, 'pages/site-A/')));
res.sendFile(path.join(__dirname, 'pages/site-A/index.html'));
} else {
app.use(express.static(path.join(__dirname, 'pages/default/')));
res.sendFile(path.join(__dirname, 'pages/default/index.html'));
}
});
While this setup works well, I need to integrate a more complex site management feature into the main site. This site has its own router, typically called like so:
app.use('/', require('pages/site/routes'));
My challenge is how to conditionally use the router file in the '/' route depending on the domain name. Here is what I have so far:
app.get('/', (req, res) => {
console.log('req.hostname: ', req.hostname)
if (req.hostname === 'localhost') {
app.use(express.static(path.join(__dirname, 'pages/site-A/')));
res.sendFile(path.join(__dirname, 'pages/site-A/index.html'));
} else if (req.hostname === 'other.local') {
// Need to find equivalent of applying router using app.use('/') here
} else {
app.use(express.static(path.join(__dirname, 'pages/default/')));
res.sendFile(path.join(__dirname, 'pages/default/index.html'));
}
});