-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdev-server.js
More file actions
29 lines (24 loc) · 848 Bytes
/
dev-server.js
File metadata and controls
29 lines (24 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = 4200;
// Virtual slug rewrite: /slug/* -> serve from root
// Any first path segment that doesn't match a real file is treated as a slug
app.use((req, res, next) => {
const seg = req.path.split('/').filter(Boolean)[0];
if (seg && !req.path.includes('.')) {
req.url = req.url.replace(`/${seg}`, '') || '/';
}
next();
});
// Serve static files from build
app.use(express.static(path.join(__dirname, 'build')));
// SPA fallback: any unmatched route -> index.html
app.get((req, res) => {
res.sendFile(path.join(__dirname, 'build/index.html'));
});
app.listen(PORT, () => {
console.log(`Dev server running at http://localhost:${PORT}`);
});