-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
72 lines (63 loc) · 1.67 KB
/
app.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
const express = require("express");
const cors = require("cors");
const app = express();
const port = 8000;
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
const shortid = require("shortid");
// 투두리스트 생성 함수
const todoGenerator = (data) => ({
id: shortid.generate(),
content: data,
checked: false,
});
// 투두리스트
const todoList = Array.from({ length: 3 }, (_, i) => todoGenerator(i));
// 투두리스트 전체 조회
app.get("/", function (req, res) {
try {
res.status(200).send(todoList);
} catch (e) {
res.status(500).send("error!");
}
});
// 투두 추가
// {content}
app.post("/", function (req, res) {
try {
const { content } = req.body;
const newTodo = todoGenerator(content);
todoList.push(newTodo);
res.status(200).send(newTodo);
} catch (e) {
res.status(500).send("error");
}
});
// 투두 수정
// {content,todoId,checked}
app.put("/", function (req, res) {
try {
const { todoId, content, checked } = req.body;
const targetIndex = todoList.findIndex((todo) => todo.id === todoId);
if (content) todoList[targetIndex].content = content;
if (checked) todoList[targetIndex].checked = checked;
res.status(200).send(todoList[targetIndex]);
} catch (e) {
res.status(500).send("error!");
}
});
// 투두 삭제
// {todoId}
app.delete("/", function (req, res) {
try {
const { todoId } = req.body;
todoList.filter((todo) => todo.id !== todoId);
res.status(200).send(todoList[targetIndex]);
} catch (e) {
res.status(500).send("error!");
}
});
app.listen(port, () => {
console.log("toy server is running at port number 8000");
});