-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
111 lines (108 loc) · 3.22 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
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
const http = require("http");
const { v4: uuidv4 } = require("uuid");
const errorHandle = require("./errorHandle");
const todoListData = [];
const requestListener = (req, res) => {
const headers = {
"Access-Control-Allow-Headers": "Content-Type, Authorization, Content-Length, X-Requested-With",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "PATCH, POST, GET,OPTIONS,DELETE",
"Content-Type": "application/json"
}
let bodyData = "";
req.on("data", chunk => {
bodyData += chunk;
})
if (req.url == "/todos" && req.method == "GET"){
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": "success",
"message": "取得待辦列表",
"data": todoListData
}))
res.end();
} else if (req.url == "/todos" && req.method == "POST") {
req.on("end", () => {
try {
const todoTitle = JSON.parse(bodyData).title;
if (!!todoTitle) {
const newTodoData = {
"title": todoTitle,
"uuid": uuidv4()
}
todoListData.push(newTodoData);
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": "success",
"message": "新增一筆待辦",
"data": todoListData
}))
res.end();
} else {
errorHandle(res);
}
} catch (error) {
errorHandle(res);
}
})
} else if (req.url == "/todos" && req.method == "DELETE") {
todoListData.length = 0;
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": "success",
"message": "刪除所有待辦",
"data": todoListData
}))
res.end();
} else if (req.url.startsWith("/todos/") && req.method == "DELETE") {
const uuid = req.url.split("/").pop();
const todoIndex = todoListData.findIndex(element => element.uuid == uuid);
if (todoIndex > -1) {
todoListData.splice(todoIndex, 1);
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": "success",
"message": "刪除一筆待辦",
"data": todoListData
}))
res.end();
} else {
errorHandle(res);
}
} else if (req.url.startsWith("/todos/") && req.method == "PATCH") {
req.on("end", () => {
try {
const editTitle = JSON.parse(bodyData).title;
const uuid = req.url.split("/").pop();
const todoIndex = todoListData.findIndex(element => element.uuid == uuid);
if (!!editTitle && todoIndex > -1) {
todoListData[todoIndex].title = editTitle;
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": "success",
"message": "編輯一筆待辦",
"data": todoListData
}))
res.end();
} else {
errorHandle(res);
}
res.end();
} catch (error) {
errorHandle(res);
}
})
} else if (req.method == "OPTIONS") {
res.writeHead(200, headers);
res.end();
} else {
res.writeHead(404, headers);
res.write(JSON.stringify({
"status": "false",
"message":"路徑錯誤"
}))
res.end();
}
}
const server = http.createServer(requestListener);
server.listen(process.env.PORT || 3005);