-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (54 loc) · 1.61 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
// server init
require('dotenv').config()
const port = process.env.MODE === 'dev' ? 3000 : process.env.PORT
const express = require('express')
const app = express()
// body parser
app.use(express.urlencoded({ extended: false, limit: '5mb' }))
app.use(express.json({ extended: false, limit: '5mb' }))
// view engine
const exphdb = require('express-handlebars')
app.engine('hbs', exphdb({ defaultLayout: 'main', extname: '.hbs' }))
app.set('view engine', 'hbs')
// static files
app.use(express.static('public'))
// socket
const http = require('http')
const server = http.createServer(app)
const { Server } = require('socket.io')
const io = new Server(server)
const { ExpressPeerServer } = require('peer')
// start peerjs server except in test mode
if (process.env.MODE !== 'test') {
const peerServer = ExpressPeerServer(server, {
proxied: true,
debug: true
})
app.use('/peerjs', peerServer)
}
// use Routes
const routes = require('./server/routes')
app.use(routes)
// handle draw history
const socketConnection = require('./server/controller/socket/socket-connection')
socketConnection(io)
// Page not found
app.use(function (req, res, next) {
res.status(404).render('404', { style: '404.css' })
})
// Error handling
app.use(function (err, req, res, next) {
const { status, error } = err
console.log('Error Handler:', error)
if (status && error) {
res.status(status).send({ error })
} else {
res.status(500).send({ error: 'Server Error' })
}
})
if(require.main === module){
server.listen(port, () => {
console.log(`This server is running on http://localhost:${port}`)
})
}
module.exports = { server }