-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
34 lines (27 loc) · 908 Bytes
/
server.js
File metadata and controls
34 lines (27 loc) · 908 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
33
34
const express = require("express");
const path = require("path");
const app = express();
// Cache middleware
const cacheControl = (req, res, next) => {
res.set('Cache-Control', 'public, max-age=31536000');
next();
};
app.use(express.static("public", {
maxAge: '1y',
setHeaders: (res, path) => {
if (path.endsWith('.html')) {
res.setHeader('Cache-Control', 'public, max-age=0');
}
}
}));
app.use(express.json());
app.get("/posts/:slug", (req, res) => {
res.sendFile(path.join(__dirname, "public", "posts", `${req.params.slug}.html`));
});
app.get("/page/:number", (req, res) => {
res.sendFile(path.join(__dirname, "public", "page", req.params.number, "index.html"));
});
app.get("*", (req, res) => {
res.status(404).sendFile(__dirname + "/public/404.html");
});
app.listen(3000, () => console.log("Server running at http://localhost:3000"));