-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (73 loc) · 2.23 KB
/
server.js
File metadata and controls
88 lines (73 loc) · 2.23 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
import express from "express";
import cors from "cors";
const app = express();
const PORT = process.env.PORT || 3000;
// Middlewares
app.use(cors());
app.use(express.json());
// In-memory data store (for demo only)
let nextId = 3;
let todos = [
{ id: 1, task: "Learn REST API", done: false },
{ id: 2, task: "Build a project", done: false },
];
// Health check
app.get("/health", (_req, res) => {
res.json({ status: "ok" });
});
// GET all todos
app.get("/todos", (_req, res) => {
res.json(todos);
});
// GET one todo by id
app.get("/todos/:id", (req, res) => {
const id = Number(req.params.id);
const todo = todos.find((t) => t.id === id);
if (!todo) return res.status(404).json({ error: "Todo not found" });
res.json(todo);
});
// POST create todo
app.post("/todos", (req, res) => {
const { task } = req.body || {};
if (!task || typeof task !== "string" || !task.trim()) {
return res.status(400).json({ error: "Field 'task' (non-empty string) is required" });
}
const newTodo = { id: nextId++, task: task.trim(), done: false };
todos.push(newTodo);
res.status(201).json(newTodo);
});
// PUT update todo
app.put("/todos/:id", (req, res) => {
const id = Number(req.params.id);
const todo = todos.find((t) => t.id === id);
if (!todo) return res.status(404).json({ error: "Todo not found" });
const { task, done } = req.body || {};
if (task !== undefined) {
if (typeof task !== "string" || !task.trim()) {
return res.status(400).json({ error: "If provided, 'task' must be a non-empty string" });
}
todo.task = task.trim();
}
if (done !== undefined) {
if (typeof done !== "boolean") {
return res.status(400).json({ error: "If provided, 'done' must be boolean" });
}
todo.done = done;
}
res.json(todo);
});
// DELETE todo
app.delete("/todos/:id", (req, res) => {
const id = Number(req.params.id);
const index = todos.findIndex((t) => t.id === id);
if (index === -1) return res.status(404).json({ error: "Todo not found" });
const [deleted] = todos.splice(index, 1);
res.json(deleted);
});
// 404 handler
app.use((_req, res) => {
res.status(404).json({ error: "Route not found" });
});
app.listen(PORT, () => {
console.log(`✅ Server running at http://localhost:${PORT}`);
});