-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (51 loc) · 1.57 KB
/
app.js
File metadata and controls
61 lines (51 loc) · 1.57 KB
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
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var db = require('./data/database.js');
app.get('/', function(req, res) {
res.sendFile(__dirname + '/templates/index.html');
});
io.on('connection', function(socket) {
socket.on('event', function(data) {
console.log('A client is connected:', data.message);
});
socket.on('vote-up', function(data){
db.voteUp(data.id)
.then((candidate) => {
socket.emit('vote-up-finished', { score: candidate.score })
})
.catch((err) => {
console.log(err);
socket.emit('vote-up-finished', { error: -1 });
});
});
socket.on('vote-down', function(data){
db.voteDown(data.id)
.then((candidate) => {
socket.emit('vote-down-finished', { score: candidate.score })
})
.catch((err) =>
socket.emit('vote-down-finished', { error: -1 })
);
});
socket.on('info-candidate', function(data){
db.getCandidateById(data.id)
.then(function(candidate){
//console.log('info-candidate', candidate);
})
.catch((err) =>
console.log(err)
);
//socket.emit('info-candidate-updated', { message: 'info-candidate-updated!' });
});
socket.on('all-candidates', function(data){
db.getAllCandidates()
.then(function(candidates){
//console.log('all-candidates', candidates);
})
.catch((err) => console.log(err));
});
});
db.setupDatabase();
server.listen(8080);
console.log('Server started on ', 8080);