-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
55 lines (45 loc) · 1.49 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
53
54
55
const fs = require("fs").promises;
const express = require("express");
const { decorateApp } = require("@awaitjs/express");
const cors = require("cors");
const morgan = require("morgan");
const app = decorateApp(express());
const corsOptions = {
origin: true,
maxAge: 3600,
methods: ["HEAD", "GET"]
};
app.use(cors(corsOptions));
app.use(morgan("combined"));
const conneg = (types) => (req, res, next) => {
const contentType = req.accepts(types);
if (!contentType) {
res.status(406).end();
} else {
res.set("Content-Type", contentType);
next();
}
};
const filename = (req) => {
const originalUrl = req.originalUrl;
const url = originalUrl[originalUrl.length - 1] === "/" ? originalUrl.slice(0, -1) : originalUrl;
return `${__dirname}/data${url}.json`;
};
const cache = async (req, res, next) => {
const resource = JSON.parse(await fs.readFile(filename(req), "utf8"));
const lastModified = new Date(resource.edited);
if (req.headers["if-modified-since"] && new Date(req.headers["if-modified-since"]) > lastModified) {
res.status(304).end();
} else {
res.set("Last-Modified", lastModified.toUTCString());
next();
}
};
app.use("*", conneg(["application/reference+json", "application/json"]));
app.useAsync("*", cache);
const year = 31536000000;
app.get("*", (req, res) => {
res.sendFile(filename(req), { maxAge: year, immutable: true });
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`swapi.hyperjump.io listening on port ${port}`));