-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.ts
67 lines (58 loc) · 1.24 KB
/
app.ts
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
import socketio from 'socket.io'
import express from 'express'
import async from 'async'
import http from 'http'
import bodyParser from 'body-parser'
import mongoose from 'mongoose'
import {
setupLiteratureGame,
LiteratureRouter
} from './src/games/literature/api'
import { config } from './config'
const port = process.env.PORT ? process.env.PORT : config.express.port
const app = express()
var io, server
mongoose.connect(
'mongodb://' +
config.mongodb.host +
':' +
config.mongodb.port +
'/' +
config.mongodb.db
)
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// catch 400
app.use((err, req, res, next) => {
console.log(err.stack)
res.status(400).send(`Error: ${String(res.originUrl)} not found`)
next()
})
// catch 500
app.use((err, req, res, next) => {
console.log(err.stack)
res.status(500).send(`Error: ${String(err)}`)
next()
})
app.use(LiteratureRouter)
server = http.createServer(app)
async.waterfall(
[
async (callback) => {
io = await socketio.listen(server)
callback()
},
async () => {
var namespace = await io.of('/literature')
setupLiteratureGame(namespace)
server.listen(port)
}
],
function (err) {
if (err) {
console.error(err)
process.exit(1)
return
}
}
)