-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
169 lines (166 loc) · 5.91 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const http = require('http');
const { v4: uuidv4 } = require('uuid');
const errorHeadle = require('./errorHeadle');
// const todos = [
// {
// title: '睡前要刷牙',
// id: uuidv4()
// }
// ]
const todos = [
{
title: '完成預習影音課程',
id: uuidv4()
},
{
title: '傍晚運動30分鐘',
id: uuidv4()
}
];
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'
// 'Content-Type': 'text/pain'
}
// res.writeHead(200,{'Content-Type':'text/pain'}),
// res.write('hello'),
// res.end()
let body = '';
req.on('data', (chunk) => {
// console.log(chunk);
body += chunk;
});
// req.on('end', () => {
// console.log(body); // 字串型態
// console.log(JSON.parse(body).title) // 字串轉成物件,才能取 title 屬性
// });
if (req.url == '/todos' && req.method == 'GET') {
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": 'success',
"data": todos
}));
res.end();
} else if (req.url == '/todos' && req.method == 'POST') {
req.on('end', () => {
try {
const title = JSON.parse(body).title;
if (title !== undefined) {
const data =
{
"title": title,
"id": uuidv4()
};
todos.push(data);
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": 'success',
"data": todos
}));
res.end();
} else {
// 寫法1:400錯誤訊息寫不同
const mess = '無此屬性';
errorHeadle(res, mess);
// 寫法2:400錯誤訊息寫相同(重複的函式未簡化)
// res.writeHead(400, headers);
// res.write(JSON.stringify({
// "status": 'false',
// "message": '欄位未填寫正確,或無此 todo id'
// }));
// res.end();
}
}
catch {
// 寫法1:400錯誤訊息寫不同
const mess = '欄位未填寫正確,或無此 todo id';
errorHeadle(res, mess);
// 寫法2:400錯誤訊息寫相同(重複的函式未簡化)
// res.writeHead(400, headers);
// res.write(JSON.stringify({
// "status": 'false',
// "message": '欄位未填寫正確,或無此 todo id'
// }));
// res.end();
};
});
} else if (req.url == '/todos' && req.method == 'DELETE') {
// 清空陣列資料
todos.length = 0;
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": 'success',
"data": todos,
"message": "delete"
}));
res.end();
} else if (req.url.startsWith('/todos/') && req.method == 'DELETE') {
const id = req.url.split("/").pop();
// console.log(id);
const idIndex = todos.findIndex(element => element.id == id);
// console.log(idIndex);
if (idIndex !== -1) {
todos.splice(idIndex, 1);
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": 'success',
"data": todos,
}));
res.end();
} else {
// 寫法1:400錯誤訊息寫不同
const mes = '查無此 todo id'
errorHeadle(res, mes);
// 寫法2:400錯誤訊息寫相同
// errorHeadle(res);
};
} else if (req.url.startsWith('/todos/') && req.method == 'PATCH') {
req.on('end', () => {
try {
const title = JSON.parse(body).title;
// console.log(title);
const id = req.url.split("/").pop();
// console.log(id);
const idIndex = todos.findIndex(element => element.id == id);
// console.log(idIndex);
if (title !== undefined && idIndex !== -1) {
// 更新陣列內指定的資料
todos[idIndex].title = title;
res.writeHead(200, headers);
res.write(JSON.stringify({
"status": 'success',
"data": todos
}));
res.end();
} else {
// 寫法1:400錯誤訊息寫不同
const mes = '查無 todo title 或 id 屬性'
errorHeadle(res, mes);
// 寫法2:400錯誤訊息寫相同
// errorHeadle(res);
};
} catch {
// 寫法1:400錯誤訊息寫不同
const mes = '欄位未填寫正確,或無此 todo id';
errorHeadle(res, mes);
// 寫法2:400錯誤訊息寫相同
// errorHeadle(res);
};
});
} else if (req.method == 'OPTIONS') {
res.writeHead(200, headers);
res.end();
} else {
res.writeHead(404, headers);
res.write(JSON.stringify({
"status": 'false',
"message": 'not found pages'
}));
res.end();
};
};
const server = http.createServer(requestListener);
server.listen(process.env.PORT || 3005);