Repository navigation
Expand file tree
/
Copy pathbackend.js
More file actions
90 lines (83 loc) · 2.5 KB
/
Copy pathbackend.js
File metadata and controls
90 lines (83 loc) · 2.5 KB
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
const https = require("https");
const fs = require("fs");
const path = require("path");
const url = require("url");
const privateKey = fs.readFileSync(path.join(__dirname, "server.key"), "utf8");
const certificate = fs.readFileSync(
path.join(__dirname, "server.cert"),
"utf8"
);
const credentials = { key: privateKey, cert: certificate };
let lastUpdateTime = undefined;
let lastData = {
id: 0,
siteId: 0,
classLessonId: 0,
createTime: null,
};
function getIndex(res) {
fs.readFile(path.join(__dirname, "index.html"), "utf8", (err, data) => {
if (err) {
res.writeHead(500, { "Content-Type": "text/plain" });
res.end("Internal Server Error");
return;
}
res.writeHead(200, { "Content-Type": "text/html" });
res.end(data);
});
}
function newData(req, res) {
let body = "";
req.on("data", (chunk) => {
body += chunk.toString();
});
req.on("end", () => {
const data = JSON.parse(body).data;
// 检查接收到的数据
const reg =
/checkwork\|id=(\d+)&siteId=(\d+)&createTime=(\d+-\d+-\d+T\d+:\d+:\d+\.\d+)&classLessonId=(\d+)/;
if (!reg.test(data)) {
res.writeHead(400, { "Content-Type": "text/plain" });
res.end("Bad Request");
return;
}
// 保存除了时间之外的数据
const match = data.match(reg);
lastData = {
id: parseInt(match[1]),
siteId: parseInt(match[2]),
createTime: new Date(match[3]),
classLessonId: parseInt(match[4]),
};
console.log(lastData);
// 时间保存为当前时间
lastUpdateTime = new Date().toISOString();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Data received", data: data }));
});
}
function getData(req, res) {
// 返回数据lastUpdateTime和lastData
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
lastUpdateTime: lastUpdateTime,
lastData: lastData,
})
);
}
const server = https.createServer(credentials, (req, res) => {
const parsedUrl = url.parse(req.url, true);
const pathname = parsedUrl.pathname;
if (pathname === "/" && req.method === "GET") getIndex(res);
else if (pathname === "/newData" && req.method === "POST") newData(req, res);
else if (pathname === "/getData" && req.method === "GET") getData(req, res);
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("Not Found");
}
});
const port = 443;
server.listen(port, () => {
console.log(`HTTPS Server running on port ${port}`);
});