-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
52 lines (43 loc) · 1.29 KB
/
server.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require("dotenv").config();
const express = require("express");
const apiRouter = require("./api/index");
const path = require("path");
const app = express();
const { Stats } = require("express-simple-stats");
const stats = Stats(process.env.expressStatsPwd);
app.use("/api/stats", stats.router);
app.use(stats.middleware);
const setNoCache = (res) => {
const date = new Date();
date.setFullYear(date.getFullYear() - 1);
res.setHeader("Expires", date.toUTCString());
res.setHeader("Pragma", "no-cache");
res.setHeader("Cache-Control", "public, no-cache");
};
const setLongTermCache = (res) => {
const date = new Date();
date.setFullYear(date.getFullYear() + 1);
res.setHeader("Expires", date.toUTCString());
res.setHeader("Cache-Control", "public, max-age=31536000, immutable");
};
app.use(
express.static(path.join(__dirname, "build"), {
extensions: ["html"],
setHeaders(res, path) {
if (path.match(/(\.html|\/sw\.js)$/)) {
setNoCache(res);
return;
}
if (path.match(/\.(js|css|png|jpg|jpeg|gif|ico|json)$/)) {
setLongTermCache(res);
}
},
})
);
app.use("/api", apiRouter);
app.get("/*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.listen(5000, () => {
console.log("App running");
});