-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
40 lines (28 loc) · 828 Bytes
/
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
const koaLoader = require('@/loaders/koa.loader.js')
const socketLoader = require('@/loaders/socket.loader.js')
const http = require('http')
const router = require('@/routes')
const send = require('koa-send')
const sockets = require('@/middleware/sockets.middleware')
const port = process.env.PORT || 3000;
(async () => {
// load Koa app
const koa = koaLoader()
// create server
const server = http.createServer(koa.callback())
// connect sockets
const io = socketLoader(server)
// TODO: replace this logic to middleware
koa.use(async (ctx, next) => {
ctx.io = io
await next()
})
// router
router(koa, io)
koa.use(sockets)
// FIXME: hot fix for handling all request to index.html
koa.use(async (ctx) => {
await send(ctx, './public/index.html')
})
server.listen(port)
})()