53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
// Serve generated static sites at /sites/{clientId}/
|
|
// (Subdomain serving is handled in server.js middleware)
|
|
|
|
const pageMap = {
|
|
'': 'index.html',
|
|
'contact': 'contact.html',
|
|
'privacy-policy': 'privacy-policy.html',
|
|
'terms': 'terms.html',
|
|
};
|
|
|
|
// Serve static assets for the site (CSS, images, etc.)
|
|
router.use('/:clientId/assets', (req, res) => {
|
|
const { clientId } = req.params;
|
|
const filePath = path.join(__dirname, '..', 'data', clientId, 'site', 'assets', req.path);
|
|
if (fs.existsSync(filePath)) {
|
|
res.sendFile(filePath);
|
|
} else {
|
|
res.status(404).send('Not found');
|
|
}
|
|
});
|
|
|
|
// Serve pages
|
|
router.get('/:clientId/:page?', (req, res) => {
|
|
const { clientId } = req.params;
|
|
const page = (req.params.page || '').replace(/\/$/, '');
|
|
const filename = pageMap[page];
|
|
|
|
if (!filename) {
|
|
return res.status(404).send('Page not found');
|
|
}
|
|
|
|
const filePath = path.join(__dirname, '..', 'data', clientId, 'site', filename);
|
|
if (!fs.existsSync(filePath)) {
|
|
return res.status(404).send('Site not found');
|
|
}
|
|
|
|
// When served via /sites/{id}/, rewrite links to include the prefix
|
|
let html = fs.readFileSync(filePath, 'utf8');
|
|
const baseUrl = `/sites/${clientId}`;
|
|
html = html.replace(/href="\//g, `href="${baseUrl}/`);
|
|
html = html.replace(/src="\//g, `src="${baseUrl}/`);
|
|
html = html.replace(/href="#/g, 'href="#'); // keep anchor links
|
|
|
|
res.type('html').send(html);
|
|
});
|
|
|
|
module.exports = router;
|