-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
49 lines (42 loc) · 1.06 KB
/
main.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
const express = require("express");
const app = express();
const port = 3000;
let todos = [];
app.use(express.json());
app.get("/todos", (req, res) => {
res.send(todos);
});
app.post("/todos", (req, res) => {
const name = req.body.name;
const description = req.body.description;
const id = todos.length;
todos.push({ name, description, id });
res.send("CREATED");
});
app.put("/todos/:id", (req, res) => {
const id = Number.parseInt(req.params.id);
const name = req.body.name;
const description = req.body.description;
let newTodos;
for (let i = 0; i < todos.length; i++) {
if (todos[i].id === id) {
if (name) {
todos[i].name = name;
}
if (description) {
todos[i].description = description;
}
newTodos = todos[i];
break;
}
}
res.send(newTodos);
});
app.delete("/todos/:id", (req, res) => {
const id = Number.parseInt(req.params.id);
todos = todos.filter((item) => item.id !== id);
res.send(todos);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});