This repository has been archived by the owner on Jun 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
60 lines (48 loc) · 1.41 KB
/
server.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
const http = require('http');
const path = require('path');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const uuidv1 = require('uuid/v1');
const helpers = require('express-helpers')(app);
const server = http.createServer(app);
const io = require('socket.io').listen(server);
const connection = require('./server/connection.js');
const rooms = [];
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/css', express.static(path.join(__dirname, 'css')));
app.use('/js', express.static(path.join(__dirname, 'js')));
app.use('/assets', express.static(path.join(__dirname, 'assets')));
connection.start(io);
app.get('/', (req, res) => {
res.render('index');
});
app.get('/games/new', (req, res) => {
res.render('new_game');
});
app.post('/games', (req, res) => {
const name = req.body.identifier;
let room = rooms.find(room => room.name === name);
if (!room) {
room = {
key: uuidv1(),
name
};
rooms.push(room);
}
res.redirect('/games/' + room.key);
});
app.get('/games/:id', (req, res) => {
res.render('game', { room: req.params.id });
});
app.get('/games', (req, res) => {
res.render('games', { rooms });
});
app.get('/instructions', (req, res) => {
res.render('instructions');
});
server.listen(process.env.PORT || 8081, () => {
console.log('Listening on ' + server.address().port);
});