-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-server.js
71 lines (64 loc) · 1.72 KB
/
test-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
var express = require('express');
const bodyParser = require('body-parser')
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
// Create server
const app = express()
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
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();
});
// Create database instance and start server
const adapter = new FileSync('database/solar.json')
const db = low(adapter)
db._.mixin({
latest: function(array) {
var long = array.length;
return array[long - 1];
}
});
// Routes
// GET /posts/:id
app.get('/latestTime/:temps', (req, res) => {
if (req.params.temps === 'temp') {
db.read();
const temp2 = db.get('temps')
.latest()
.get('data')
.find({name: 'Temperature sensor 2'})
.value()
const temp1 = db.get('temps')
.latest()
.get('data')
.find({name: 'Temperature sensor 1'})
.value()
const time = db.get('temps')
.latest()
.get('time')
.value()
const date = db.get('temps')
.latest()
.get('date')
.value()
const test = [temp1, temp2, time, date]
res.send(test);
} else {
res.status(404)
.send('Cannot GET /latestTime/' + req.params.temps);
}
})
// POST /posts
app.post('/posts', (req, res) => {
db.get('temps')
.push(req.body)
.last()
.assign({ id: Date.now().toString() })
.write()
res.send(post)
})
var server = app.listen(3000);
console.log('Example app listening at port 3000');
module.exports = server;