-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (72 loc) · 2.19 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Initialize express
let express = require('express');
let app = express();
app.use('/', express.static('public'));
// Initialize body-parser
let bodyParser = require('body-parser');
app.use(bodyParser.json());
// Initialize nedb
let Datastore = require('nedb');
let db = new Datastore('breathData.db');
db.loadDatabase();
// Initialize http
let http = require('http');
let server = http.createServer(app);
let port = process.env.PORT || 3000;
server.listen(port, () => {
console.log('Server listening at port: ' + port);
});
app.get('/getBreaths/:user', (req, res) => {
let userName = req.params.user
console.log(userName);
db.find({"name" : userName}, (err, docs) => {
if (err) {
res.json({"task" : "failed"});
} else {
let obj = {data : docs};
res.json(obj);
}
});
});
// Process GET requests for Client data initialization
app.get('/getBreaths', (req, res) => {
db.find({}, (err, docs) => {
if (err) {
res.json({"task" : "failed"});
} else {
let obj = {data : docs};
res.json(obj);
}
});
});
// Process POST requests and insert data whenever a user completes a breath
app.post('/sendBreaths', (req, res) => {
console.log(req.body.name);
db.find({"name" : req.body.name}, (err, docs) => {
console.log(docs);
if (!docs[0]) {
db.insert(req.body, (err, newDocs) => {
if (err) {
res.json('failed');
} else {
console.log("new user data added");
res.json({"task" : "success"});
}
});
} else {
db.update({"name" : req.body.name}, req.body, {multi: true}, (err, numReplaced) => {
console.log("userData updated");
});
}
})
});
// Initialize Socket
let io = require('socket.io').listen(server);
// Listen for Individual Connection
io.sockets.on('connection', function(socket) {
console.log ('New client: ' + socket.id);
socket.on('msg', function(newBreaths) {
// console.log(userObject);
socket.broadcast.emit('msg', newBreaths);
});
});