-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
145 lines (136 loc) · 3.84 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express')
const PORT = process.env.PORT || 5000
var app = express();
var fire = require('./fire')
var cors = require('cors');
var bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', (req, res) => {
res.send(
'<h1>Tes Express & Firebase Cloud Firestore</h1><ul><li><p><b>GET /data/esp8266</b></p></li><li><p><b>GET /data/esp32</b></p></li><li><p><b>GET /data/mkr1000</b></p></li><li><p><b>POST /data/esp8266</b> => {suhu, lembab, analog}</p></li><li><p><b>POST /data/esp32</b> => {suhu, lembab, analog}</p></li><li><p><b>POST /data/mkr1000</b> => {suhu, lembab, analog}</p></li></ul>')
})
app.get('/data/esp8266', (req, res) => {
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
var wholeData = []
db.collection('lin_esp8266').orderBy('waktu', 'asc').get()
.then(snapshot => {
snapshot.forEach(doc => {
// console.log(doc.id, '=>', doc.data());
// console.log(doc.data().name + doc.data().age);
// console.log(doc.data());
wholeData.push(doc.data())
});
console.log(wholeData)
res.send(wholeData)
})
.catch(error => {
console.log('Error!', error);
})
})
app.post('/data/esp8266', (req, res)=>{
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
db.collection('lin_esp8266').add({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date()
});
res.send({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date(),
status: 'POST data sukses!'
})
})
app.get('/data/esp32', (req, res) => {
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
var wholeData = []
db.collection('lin_esp32').orderBy('waktu', 'asc').get()
.then(snapshot => {
snapshot.forEach(doc => {
// console.log(doc.id, '=>', doc.data());
// console.log(doc.data().name + doc.data().age);
// console.log(doc.data());
wholeData.push(doc.data())
});
console.log(wholeData)
res.send(wholeData)
})
.catch(error => {
console.log('Error!', error);
})
})
app.post('/data/esp32', (req, res)=>{
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
db.collection('lin_esp32').add({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date()
});
res.send({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date(),
status: 'POST data sukses!'
})
})
app.get('/data/mkr1000', (req, res) => {
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
var wholeData = []
db.collection('lin_mkr1000').orderBy('waktu', 'asc').get()
.then(snapshot => {
snapshot.forEach(doc => {
// console.log(doc.id, '=>', doc.data());
// console.log(doc.data().name + doc.data().age);
// console.log(doc.data());
wholeData.push(doc.data())
});
console.log(wholeData)
res.send(wholeData)
})
.catch(error => {
console.log('Error!', error);
})
})
app.post('/data/mkr1000', (req, res)=>{
const db = fire.firestore();
db.settings({
timestampsInSnapshots: true
});
db.collection('lin_mkr1000').add({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date()
});
res.send({
suhu: req.body.suhu,
lembab: req.body.lembab,
analog: req.body.analog,
waktu: new Date(),
status: 'POST data sukses!'
})
})
app.listen(PORT, () => {
console.log(`Listening on ${ PORT }`)
})