Fix root path routing - serve index.html for /

This commit is contained in:
Mikael Westöö
2026-01-23 21:45:38 +01:00
parent 42993ced70
commit e0ad2bc49e

View File

@@ -47,25 +47,33 @@ app.use(session({
} }
})); }));
// Serve static files first
app.use(express.static('public'));
// Middleware to handle routes without .html extension // Middleware to handle routes without .html extension
app.use((req, res, next) => { app.use((req, res, next) => {
if (req.path.indexOf('.') === -1 && !req.path.startsWith('/api/') && !req.path.startsWith('/admin/')) { // Skip API routes
if (req.path.startsWith('/api/')) {
return next();
}
// Handle root path
if (req.path === '/') {
return res.sendFile(path.join(__dirname, 'public', 'index.html'));
}
// Handle paths without extensions (not already handled by static middleware)
if (req.path.indexOf('.') === -1) {
const file = `${req.path}.html`; const file = `${req.path}.html`;
res.sendFile(path.join(__dirname, 'public', file), (err) => { return res.sendFile(path.join(__dirname, 'public', file), (err) => {
if (err) next(); if (err) {
});
} else if (req.path.indexOf('.') === -1 && req.path.startsWith('/admin/') && req.path !== '/admin/') {
const file = `${req.path}.html`;
res.sendFile(path.join(__dirname, 'public', file), (err) => {
if (err) next();
});
} else {
next(); next();
} }
}); });
}
// Serve static files next();
app.use(express.static('public')); });
// Configure multer for file uploads (memory storage for CV uploads) // Configure multer for file uploads (memory storage for CV uploads)
const upload = multer({ const upload = multer({