This repository has been archived by the owner on Feb 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
71 lines (58 loc) · 2.04 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
//Module requirements
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var bodyparser = require('body-parser');
var app = express();
app.use(express.static('client'));
var db; // Initialiseren van mongoDB variabele naam
var dataTable; //Opslagen van data in formaat
MongoClient.connect('mongodb://localhost/testmaarten', function (err, _db){
if (err) throw err;
if (err) console.log(err);
console.log("Connected to database");
db = _db
dataTable = db.collection('data');
dataTable.find().each(function(err, doc){
console.log(doc); //logt een null nadat alle data is weergeven
//TODO Fix ^ bug
});
});
//MOGELIJKE BEMERKINGEN
//Data weergeven. Naam in textbox? > toon vakje met naam , bpm en laatste tijd
//Hoe constant automatisch refreshen?
app.use(bodyparser.json());
//Break database connection
process.on('SIGINT', function(){
console.log("Server wordt afgesloten");
db.close();
process.exit(0);
});
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
app.get('/api/data', function(req, res){
dataTable.find().toArray(function (err, data){
if (err) throw err;
res.status(200).json(data);
})
});
//TODO filter out duplicate voornaam + achternaam
//Gebruiker maarten wachters alleen BPM updaten en tonen
app.post('/api/data', function(req, res){
console.log(req.body);
data = {'timestamp': Date.now(), 'voornaam': req.body.voornaam, 'achternaam': req.body.achternaam, 'bpm': req.body.bpm};
console.log("entry created");
dataTable.insert(data, function (err, result){
if (err) throw err;
//console.log(err);
dataTable.find().toArray(function (err, data){
if (err) throw err;
//console.log(err);
res.status(201).json("data received");
});
});
});
app.listen(3000);