-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
94 lines (75 loc) · 2.44 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var fs = require('fs');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var MOZ_FILE = 'moz-data.json';
var SAMPLE_FILE = 'sample-data.json';
var filename = process.env.MOZ? MOZ_FILE : SAMPLE_FILE;
var data_file = path.join(__dirname, filename);
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
// Additional middleware which will set headers that we need on each request.
app.use(function (req, res, next) {
// Set permissive CORS header - this allows this server to be used only as
// an API server in conjunction with something like webpack-dev-server.
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
app.get('/api/feud-data', function (req, res) {
fs.readFile(data_file, function (err, data) {
if (err) {
console.error(err);
process.exit(1);
}
res.json(JSON.parse(data));
});
});
app.post('/api/feud-data', function (req, res) {
fs.readFile(data_file, function (err, data) {
if (err) {
console.error(err);
process.exit(1);
}
var questions = JSON.parse(data);
var which_question = req.body.question;
questions[which_question].counter += 1;
fs.writeFile(data_file, JSON.stringify(questions, null, 2), function(err) {
if (err) {
console.error(err);
process.exit(1);
}
});
});
});
app.get('/', function (req, res) {
res.sendfile('index.html');
});
io.on('connection', function (socket) {
console.log('a user connected');
socket.on('hide answers', function (state) {
socket.broadcast.emit('update game', state);
});
socket.on('reveal answer', function (react_id) {
socket.broadcast.emit('reveal answer', react_id);
});
socket.on('update game', function (state) {
socket.broadcast.emit('update game', state);
});
socket.on('trigger strike', function (strike_count) {
socket.broadcast.emit('trigger strike', strike_count);
});
socket.on('trigger correct', function () {
socket.broadcast.emit('trigger correct');
});
socket.on('toggle logo flip', function (hideAnswers) {
socket.broadcast.emit('toggle logo flip', hideAnswers);
});
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
http.listen(process.env.PORT || 3000)