-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (54 loc) · 1.46 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
'use strict'
const express = require('express')
const http = require('http')
const path = require('path')
const logger = require('morgan')
const Primus = require('primus')
const expressSession = require('express-session')
const primusEmit = require('primus-emit')
/**
* Configure Express server
*/
const app = express()
const server = http.createServer(app)
const session = expressSession({
secret: 'shhhhhhhh be quiet',
resave: true,
saveUninitialized: true,
})
app.set('port', process.env.PORT || 3000)
app.set('views', path.join(__dirname, 'views'))
app.set('view engine', 'ejs')
app.use(logger('dev'))
app.use(express.static(path.join(__dirname, 'public')))
app.use(session)
/**
* Configure Primus
*/
const primus = new Primus(server, {
transformer: 'websockets',
})
primus.use('session', session)
primus.plugin('emit', primusEmit)
/**
* Controllers (route handlers and event listeners)
*/
const homeController = require('./controllers/home')
const gameController = require('./controllers/game')(primus)
/**
* Express routes
*/
app.get('/', homeController.index)
/**
* Primus listeners
*/
primus.on('connection', gameController.connection)
primus.on('disconnection', gameController.disconnection)
primus.on('error', gameController.error)
/**
* Start Express server
*/
server.listen(app.get('port'), () => {
console.log('Server started at http://localhost:%d in %s mode', app.get('port'), app.get('env'))
console.log('Press CTRL-C to stop\n')
})