-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
125 lines (103 loc) · 3.72 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
const path = require('path');
const express = require("express");
const app = express();
const fs = require('fs');
const bodyParser = require('body-parser');
const jsonParser = bodyParser.json();
const firestore = require('./myModules/DHT11Firebase-master/dht11firebase.js').Firestore;
const serviceAccount = require("./myModules/key/serviceAccountKey.json");
const myFirestore = new firestore(serviceAccount);
const port = process.env.PORT || 3000;
let nowDHT11Data = {
Name: "DHT11NowData",
Year: 2021,
Month: 10,
Day: 18,
Hour: 12,
Min: 0,
Temp: 28,
Hun: 50
};
appGetByFile("/", "/login/index.html", "html" );
appGetByFile("/login/style", "/login/style.css", "css" );
appGetByFile("/login/main", "/login/main.js", "javascript");
appGetByFile("/home", "/home/index.html", "html" );
appGetByFile("/home/style", "/home/style.css", "css" );
appGetByFile("/home/main", "/home/main.js", "javascript");
appGetByFile("/home/main1", "/home/main1.js", "javascript");
appGetByFile("/home/main2", "/home/main2.js", "javascript");
appGetByFile("/home/main3", "/home/main3.js", "javascript");
function appGetByFile(getPath, filePath, headType) {
app.get(getPath, (req, res) => {
const pblicDirectory = path.join(process.cwd(), 'public');
const data = fs.readFileSync(pblicDirectory + filePath, "utf8");
const bufferLen = Buffer.byteLength(data, 'utf8');
let myHead = {
"Content-Type": `text/${headType}; charset=UTF-8`,
"Content-Length": bufferLen
};
res.writeHead(200, myHead);
res.write (data);
res.end ();
});
}
app.post('/data/checkIdPasswd', jsonParser, async (req, res) => {
console.log("POST[Check Id Passwd]:");
console.log(req.body);
res.writeHead(200, {"Content-Type": "text/json"});
await myFirestore.checkIdPasswd(req.body)
.then(success => {
console.log("send:");
console.log(success);
res.write(JSON.stringify(success));
res.end();
})
.catch(fail => {
console.log("Error:");
console.log(fail);
res.end();
});
});
app.post("/data/setDHT11Data", jsonParser, (req, res) => {
console.log("POST[Set DHT11 Data]:");
console.log(req.body);
res.writeHead(200, {"Content-Type": "text/json"});
myFirestore.setDHT11Data(req.body)
.then(success => {
nowDHT11Data = req.body;
console.log("Send:");
console.log(success);
res.write(JSON.stringify(success));
res.end();
})
.catch(fail => {
console.log("Error:");
console.log(fail);
res.end();
});
});
app.post('/data/getDHT11Data', jsonParser, async (req, res) => {
console.log("POST[Get DHT11 Data]:");
console.log(req.body);
res.writeHead(200, {"Content-Type": "text/json"});
await myFirestore.getDHT11Data(req.body)
.then(success => {
console.log("Send:");
console.log(success);
res.write(JSON.stringify(success));
res.end();
})
.catch(fail => {
console.log("Error:");
console.log(fail);
res.end();
});
});
app.post('/data/getNowDHT11Data', jsonParser, async(req, res) => {
console.log("POST[Get Now DHT11 Data]:");
console.log(req.body);
res.writeHead(200, {"Content-Type": "text/json"});
res.write(JSON.stringify(nowDHT11Data));
res.end();
});
app.listen(port);