-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cjs
31 lines (25 loc) · 891 Bytes
/
server.cjs
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
const express = require('express');
const path = require('path');
const fs = require('fs');
const port = 3000;
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// Serve static files from the '_site' directory
app.use(express.static(path.join(__dirname, '_site')));
// Dynamic route handler to serve .html files
app.get('/:page', (req, res) => {
const { page } = req.params;
const filePath = path.join(__dirname, '_site', `${page}.html`);
// Check if the requested .html file exists
if (page && fs.existsSync(filePath)) {
// Send the requested .html file
res.sendFile(filePath);
} else {
// If the requested .html file does not exist, send the 404 page
res.status(404).sendFile(path.join(__dirname, '_site', '404.html'));
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});