-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (43 loc) · 1015 Bytes
/
index.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
/**
* Axel Boberg © 2020
*/
require('dotenv').config()
const http = require('http')
const middleware = require('./lib/middleware')
const express = require('express')
const app = express()
const listeners = require('./lib/listeners')
const room = require('./lib/room')
const io = require('./lib/io')
const PORT = process.env.PORT || 80
const server = http.createServer(app)
// Init socket and rooms
io.setup(server)
room.setInstance(io.getInstance())
listeners.setInstance(io.getInstance())
// Init middleware
middleware(app)
/**
* Handle Express errors
*/
app.use((err, req, res, next) => {
if (process.env.NODE_ENV === 'development') {
console.error(err)
}
if (!err.status || !err.message) {
err = {
status: 500,
message: 'Internal server error'
}
}
res
.status(err.status)
.end(JSON.stringify({
code: err.code,
message: err.message,
status: err.status
}))
})
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})