-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (60 loc) · 1.58 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
import express from 'express';
import cors from 'cors';
import { JSONFilePreset } from 'lowdb/node';
// Create server
const app = express();
const PORT = process.env.PORT || 3000;
// Use express json middleware to automatically parse JSON
app.use(express.json(), cors());
// Set up LowDB
const db = await JSONFilePreset('db.json', { todos: [] })
// Initialize the database
const initDB = async () => {
await db.read();
db.data ||= { todos: [] }; // If db.json doesn't exist, create todos array
await db.write();
};
// Routes
// Get all todos
app.get('/todos', async (req, res) => {
await db.read();
res.json(db.data.todos);
});
// Create a new todo
app.post('/todos', async (req, res) => {
const todo = {
id: Date.now(),
title: req.body.title,
completed: false,
};
db.data.todos.push(todo);
await db.write();
res.status(201).json(todo);
});
// Update a todo
app.patch('/todos/:id', async (req, res) => {
const { id } = req.params;
const { completed } = req.body;
await db.read();
const todo = db.data.todos.find(t => t.id == id);
if (todo) {
todo.completed = completed;
await db.write();
res.json(todo);
} else {
res.status(404).json({ message: "Todo not found" });
}
});
// Delete a todo
app.delete('/todos/:id', async (req, res) => {
const { id } = req.params;
await db.read();
const todos = db.data.todos.filter(t => t.id != id);
db.data.todos = todos;
await db.write();
res.status(204).send();
});
// Start server
initDB().then(() => {
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
});