-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
86 lines (67 loc) · 1.83 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
const Koa = require("koa")
const bodyParser = require("koa-bodyparser")
const Router = require("koa-router")
const logger = require("koa-logger")
const serve = require("koa-static")
const mongoose = require("mongoose")
const config = require("./utils/config")
const mongoUrl = config.mongoUrl
mongoose.connect(mongoUrl, { useNewUrlParser: true })
const app = new Koa()
app.use(serve(__dirname + "/frontend/build"))
app.use(bodyParser())
app.use(logger())
app.use(async (ctx, next) => {
try {
await next()
} catch (err) {
ctx.status = err.status || 500
ctx.body = err.message
ctx.app.emit("error", err, ctx)
}
})
app.on("error", (err, ctx) => {
if (err.name === "CastError") {
ctx.status = 400
} else {
console.error(err)
}
})
const router = new Router({
prefix: "/api",
})
const gamesRouter = new Router({
prefix: "/api/games",
})
const sessionsRouter = new Router({
prefix: "/api/sessions",
})
const timePeriodsRouter = new Router({
prefix: "/api/timeperiods",
})
const loginRouter = new Router({
prefix: "/api/login",
})
const syncRouter = new Router({
prefix: "/api/sync",
})
require("./routes/basic")({ router })
require("./routes/games")({ gamesRouter })
require("./routes/sessions")({ sessionsRouter })
require("./routes/login")({ loginRouter })
require("./routes/sync")({ syncRouter })
require("./routes/timeperiods")({ timePeriodsRouter })
app.use(router.routes())
app.use(router.allowedMethods())
app.use(gamesRouter.routes())
app.use(gamesRouter.allowedMethods())
app.use(sessionsRouter.routes())
app.use(sessionsRouter.allowedMethods())
app.use(timePeriodsRouter.routes())
app.use(timePeriodsRouter.allowedMethods())
app.use(loginRouter.routes())
app.use(loginRouter.allowedMethods())
app.use(syncRouter.routes())
app.use(syncRouter.allowedMethods())
if (!module.parent) app.listen(config.port)
module.exports = app