-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (25 loc) · 881 Bytes
/
Copy pathserver.js
File metadata and controls
32 lines (25 loc) · 881 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
30
31
32
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = 3000;
app.use(express.static(path.join(__dirname, 'out')));
// Dynamically create routes for all HTML files
const outDir = path.join(__dirname, 'out');
if (fs.existsSync(outDir)) {
const htmlFiles = fs.readdirSync(outDir).filter(file => file.endsWith('.html'));
htmlFiles.forEach(file => {
const routeName = file.replace('.html', '');
const routePath = routeName === 'index' ? '/' : `/${routeName}`;
app.get(routePath, (req, res) => {
res.sendFile(path.join(outDir, file));
});
});
}
// 404 handler for all other routes
app.get('*', (req, res) => {
res.status(404).sendFile(path.join(__dirname, 'out', '404.html'));
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});