This repository was archived by the owner on May 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
65 lines (53 loc) · 1.6 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
65
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const path = require('path');
var data = {
touches : 0,
vibrs : 0,
total : 0
};
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(express.static(path.join(__dirname, 'static')));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.render('index.pug', data);
});
app.get('/info', (req, res) => {
res.json(data);
});
app.post('/update', (req, res) => {
let touches = req.body.touches;
let vibrs = req.body.vibrs;
let total = req.body.total;
let first_connected = req.body.first_connected;
if (Number.isInteger(touches) &&
Number.isInteger(vibrs) &&
Number.isInteger(total) &&
typeof(first_connected) == 'boolean') {
data.touches = touches;
data.vibrs = vibrs;
data.total = total;
if (first_connected) {
io.emit('first_connected', data);
} else {
io.emit('update', data);
}
res.json({
status : 'okay',
time : new Date(),
});
} else {
res.json({
status : 'failed',
error : 'some variables are undefined or have incorrect type',
time : new Date()
});
}
});
server.listen(process.env.PORT || 5000);