-
Notifications
You must be signed in to change notification settings - Fork 63
/
app.js
90 lines (78 loc) · 2.41 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (c) 2016 Grant Miner */
"use strict";
const Readable = require("stream").Readable;
// https://github.com/koajs/koa/blob/v2.x/docs/api/index.md
const Koa = require("koa");
const app = (module.exports = new Koa());
const config = require("../../../config/web.js");
const session = require("koa-session");
const Compress = require("koa-compress");
const helmet = require("koa-helmet");
const Morgan = require("koa-morgan");
const proxy = require("koa-proxy");
const Conditional = require("koa-conditional-get");
const Etag = require("koa-etag");
const ResponseTime = require("koa-response-time");
const BodyParser = require("koa-bodyparser");
const ServeStatic = require("koa-static");
if (config.proxy) {
app.use(
proxy({
host: config.proxy,
match: /^(?!\/api)(?!\/app)/ // ...everything except /api and /app
})
);
}
app.use(ResponseTime());
app.use(Conditional());
app.use(Etag());
// app.use(Morgan('combined'));
const koaBunyanLogger = require("koa-bunyan-logger");
app.use(koaBunyanLogger());
app.use(koaBunyanLogger.requestIdContext());
app.use(koaBunyanLogger.requestLogger());
app.use(helmet.frameguard());
app.use(helmet.xssFilter());
app.use(helmet.ieNoOpen());
app.use(Compress());
app.keys = config.cookieKeys;
// Signed-cookies session support
// app.use(session({
// maxage: null
// }, app));
app.use(BodyParser());
// turn errors into a JSON structure
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
// some errors will have .status
// however ctx is not a guarantee
if (err != null) {
ctx.status = err.status || 500;
ctx.type = "application/json";
ctx.body = JSON.stringify({
success: false,
message: err.toString()
});
// since we handled ctx manually we'll
// want to delegate to the regular app
// level error handling as well so that
// centralized still functions correctly.
ctx.app.emit("error", err, ctx);
} else {
nullOrUndefined = err === null ? "null" : "undefined";
ctx.status = 500;
ctx.type = "application/json";
ctx.body = JSON.stringify({
success: false,
message: `${nullOrUndefined} error`
});
ctx.app.emit("error", new Error(`${nullOrUndefined} error`), ctx);
}
}
});
const router = require("./routes/router").router;
app.use(router.routes());
app.use(router.allowedMethods());
app.use(ServeStatic("../../public"));