This repository was archived by the owner on Mar 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (37 loc) · 1.2 KB
/
server.js
File metadata and controls
44 lines (37 loc) · 1.2 KB
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
const express = require("express");
const cors = require("cors");
const morgan = require("morgan");
const compression = require("compression");
const app = express();
const corsOptions = {
origin: true,
maxAge: 3600,
methods: ["HEAD", "GET"]
};
app.use(cors(corsOptions));
app.use(morgan("combined"));
app.use(compression());
const conneg = (types) => (req, res, next) => {
const contentType = req.accepts(types);
if (!contentType) {
res.status(406).end();
} else {
res.set("Content-Type", contentType);
next();
}
};
app.get("/common", conneg(["application/reference+json"]));
app.use("/common/*", conneg(["application/validation+json"]));
const year = 31536000000;
app.get("*", (req, res) => {
try {
const template = require(`./templates${req.path}`);
const scheme = "cf-visitor" in req.headers ? JSON.parse(req.headers["cf-visitor"])["scheme"] : req.protocol;
res.setHeader("Cache-Control", `public, max-age=${year}, immutable`)
res.send(template({ origin: `${scheme}://${req.headers.host}` }));
} catch (e) {
res.status(404).end();
}
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`validation.hyperjump.io listening on port ${port}`));