-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathserver.js
60 lines (50 loc) · 1.36 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
56
57
58
59
60
require("newrelic");
const express = require("express");
const fs = require("fs");
const Mustache = require("mustache");
const logger = require("pino")();
const path = require("path");
const cacheControl = require("express-cache-controller");
const app = express();
const indexHtml = renderIndexHtml();
app.use(
cacheControl({
noCache: true
})
);
app.get("/healthz", function(req, res, next) {
res.sendStatus(200);
next();
});
app.use(express.static(path.join(__dirname, "dist")));
app.use((req, res, next) => {
if (req.path.includes("healthz")) {
next();
return;
}
res.send(indexHtml).status(200);
next();
});
app.listen(8080, () => {
logger.info("listening on 8080");
});
function renderIndexHtml() {
let template = "";
const indexPath = path.join(__dirname, "index.html");
try {
template = fs.readFileSync(indexPath, "utf8");
} catch (err) {
logger.error(`error reading index.html file: ${err}`);
process.exit(1);
}
// speeds up rendering on the first request
Mustache.parse(template);
const config = {
zwibblerUrl: process.env.VUE_APP_ZWIBBLER_URL,
websocketRoot: process.env.VUE_APP_WEBSOCKET_ROOT,
serverRoot: process.env.VUE_APP_SERVER_ROOT,
socketAddress: process.env.VUE_APP_SOCKET_ADDRESS,
mainWebsiteUrl: process.env.VUE_APP_MAIN_WEBSITE_URL
};
return Mustache.render(template, config);
}