-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
122 lines (97 loc) · 2.88 KB
/
Copy pathserver.js
File metadata and controls
122 lines (97 loc) · 2.88 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
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
// todo backend using pure node.js
const http = require("http");
const fs = require("fs");
const url = require("url");
const PORT = 3000;
// ---------- helpers ----------
function readTodos() {
const data = fs.readFileSync("todos.json", "utf8");
return JSON.parse(data);
}
function writeTodos(todos) {
fs.writeFileSync("todos.json", JSON.stringify(todos, null, 2));
}
function getNextId(todos) {
if (todos.length === 0) return 1;
return todos[todos.length - 1].id + 1;
}
// ---------- server ----------
const server = http.createServer((req, res) => {
const parsedUrl = url.parse(req.url, true);
const method = req.method;
const path = parsedUrl.pathname;
res.setHeader("Content-Type", "application/json");
// ✅ READ all todos
if (method === "GET" && path === "/todos") {
const todos = readTodos();
res.end(JSON.stringify(todos));
}
// ✅ CREATE todo
else if (method === "POST" && path === "/todos") {
let body = "";
req.on("data", chunk => body += chunk);
req.on("end", () => {
const todos = readTodos();
const data = JSON.parse(body);
const newTodo = {
id: getNextId(todos),
title: data.title,
completed: false
};
todos.push(newTodo);
writeTodos(todos);
res.end(JSON.stringify({ message: "Todo added", todo: newTodo }));
});
}
// ✅ UPDATE todo (by id)
else if (method === "PUT" && path === "/todos") {
let body = "";
req.on("data", chunk => body += chunk);
req.on("end", () => {
const data = JSON.parse(body);
const todos = readTodos();
let found = false;
const updatedTodos = todos.map(todo => {
if (todo.id === data.id) {
found = true;
return {
...todo,
title: data.title ?? todo.title,
completed: data.completed ?? todo.completed
};
}
return todo;
});
if (!found) {
res.statusCode = 404;
return res.end(JSON.stringify({ error: "Todo not found" }));
}
writeTodos(updatedTodos);
res.end(JSON.stringify({ message: "Todo updated" }));
});
}
// ✅ DELETE todo (by id)
else if (method === "DELETE" && path === "/todos") {
let body = "";
req.on("data", chunk => body += chunk);
req.on("end", () => {
const { id } = JSON.parse(body);
const todos = readTodos();
const newTodos = todos.filter(todo => todo.id !== id);
if (todos.length === newTodos.length) {
res.statusCode = 404;
return res.end(JSON.stringify({ error: "Todo not found" }));
}
writeTodos(newTodos);
res.end(JSON.stringify({ message: "Todo deleted" }));
});
}
// ❌ invalid route
else {
res.statusCode = 404;
res.end(JSON.stringify({ error: "Route not found" }));
}
});
server.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});