-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
52 lines (46 loc) · 1.47 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
/**
* This is a sample server that can be used to run the NekoCap website with HTTP2
* Define SSL_KEY_PATH, SSL_CERT_PATH, SSL_CA_PATH in a .env file.
*/
const express = require("express");
const compression = require("compression");
const helmet = require("helmet");
const cors = require("cors");
const next = require("next");
const fs = require("fs");
const spdy = require("spdy");
const dotenv = require("dotenv");
const PORT = 443;
const env = dotenv.config({ path: "./.env" }).parsed;
const app = next({ dev: false, hostname: env.NEXT_HOSTNAME, port: PORT });
const handle = app.getRequestHandler();
const options = {
key: fs.readFileSync(env.SSL_KEY_PATH, "utf8"),
cert: fs.readFileSync(env.SSL_CERT_PATH, "utf8"),
ca: [fs.readFileSync(env.SSL_CA_PATH, "utf8")],
};
const server = express();
server.use(helmet({ contentSecurityPolicy: false, frameguard: false }));
server.use(cors());
server.use(compression());
const CACHED_PATH_SUFFIXES = [
".woff2",
".wasm",
"/subtitles-octopus-worker.js",
"/subtitles-octopus-worker-legacy.js",
];
app.prepare().then(() => {
server.all("*", (req, res) => {
if (!!req.path && isCachedPath(req.path)) {
res.setHeader("Cache-Control", "public,max-age=31536000");
}
return handle(req, res);
});
spdy.createServer(options, server).listen(PORT);
});
function isCachedPath(path) {
const suffixIsCacheable = CACHED_PATH_SUFFIXES.some((suffix) => {
return path.endsWith(suffix);
});
return suffixIsCacheable;
}