-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.js
executable file
·69 lines (56 loc) · 1.95 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
61
62
63
64
65
66
67
68
69
"use strict";
const http = require("http");
const util = require("./lib/util");
const server = require("./lib/server");
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const compression = require("compression");
const morgan = require("morgan");
server.init({});
server.onRequest = server.onRequest.bind(server);
morgan.token("request_id", function(req, res) {
const rhdr = process.env.REQUEST_ID_HEADER;
if (rhdr && req.headers[rhdr]) {
return req.headers[rhdr];
}
return "unknown";
});
morgan.token("cache_type", function(req, res) {
if (req.prerender) {
return req.prerender.cacheHit ? "CACHE_HIT" : "CACHE_MISS";
}
return "-";
});
morgan.token("prerender_url", function(req, res) {
return (req.prerender && req.prerender.url) || "-";
});
app.use(
morgan(
`:date[iso] method=:method status=:status url=:url prerender_url=:prerender_url cache_type=:cache_type timing=:response-time[0] referrer=":referrer" user_agent=":user-agent" request_id=:request_id`
)
);
app.disable("x-powered-by");
app.use(compression());
app.get("*", server.onRequest);
//dont check content-type and just always try to parse body as json
app.post("*", bodyParser.json({ type: () => true }), server.onRequest);
app.use(function(err, req, res, next) {
res.status(500).end();
util.log(`Unhandled request error error=${err} stack=${err.stack}`);
});
server.use(require("./lib/plugins/healthCheckAuth"));
server.use(require("./lib/plugins/basicAuth"));
server.use(require("./lib/plugins/blockResources"));
server.use(require("./lib/plugins/blacklist"));
server.use(require("./lib/plugins/removeScriptTags"));
server.use(require("./lib/plugins/httpHeaders"));
server.use(require("./lib/plugins/s3HtmlCache"));
server.start();
process.on("SIGHUP", () => {
server.gracefulBrowserRestart();
});
const port = process.env.PORT || 3000;
app.listen(port, () =>
util.log(`Prerender server accepting requests on port ${port}`)
);