-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (45 loc) · 1.46 KB
/
index.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
var express = require('express');
var bodyParser = require('body-parser');
var expressHbs = require('express-handlebars');
var socket = require('./socket.js').socket;
var socket_send = require('./socket.js').send;
var onConnection = require('./socket.js').onConnection;
var sockets = express();
var sockets_server = require('http').createServer(sockets);
socket.installHandlers(sockets_server, {prefix: '/socket'});
sockets_server.listen(1338, '0.0.0.0');
var app = express();
var router = express.Router();
app.use(bodyParser.text());
app.set('views', __dirname + '/views');
app.engine('hbs', expressHbs({extname:'hbs'}));
app.set('view engine', 'hbs');
var message = "No messages yet";
router.use(function(req, res, next) {
console.log('%s %s %s', req.method, req.url, req.path);
next();
});
router.use(express.static(__dirname + '/static'));
router.get('/', function(req, res) {
res.render('home', {message: message});
});
var points = {'1':0, '2':0, '3':0, '4':0};
router.post('/', function(req, res) {
var alternative = req.body;
if (alternative in points) {
points[alternative]++;
socket_send(JSON.stringify(points));
}
res.send('Updated!');
});
router.post('/reset/', function(req, res) {
points = {'1':0, '2':0, '3':0, '4':0};
socket_send(JSON.stringify(points));
res.send('Updated!');
});
app.use('/', router);
app.listen(1337);
console.log("It's at port 1337!");
onConnection(function(connection) {
socket_send(JSON.stringify(points));
});