Fix root path routing - serve index.html for /
This commit is contained in:
38
server.js
38
server.js
@@ -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
|
||||||
const file = `${req.path}.html`;
|
if (req.path.startsWith('/api/')) {
|
||||||
res.sendFile(path.join(__dirname, 'public', file), (err) => {
|
return next();
|
||||||
if (err) next();
|
|
||||||
});
|
|
||||||
} 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();
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Serve static files
|
// Handle root path
|
||||||
app.use(express.static('public'));
|
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`;
|
||||||
|
return res.sendFile(path.join(__dirname, 'public', file), (err) => {
|
||||||
|
if (err) {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
|
||||||
// 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({
|
||||||
|
|||||||
Reference in New Issue
Block a user