-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.js
82 lines (71 loc) · 1.86 KB
/
server2.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
77
78
79
80
81
82
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs/promises');
const app = express();
const PORT = 3000;
const JSON_FILE = 'todos.json';
app.use(bodyParser.json());
let todos = [];
// Load todos from the JSON file
async function loadTodos() {
try {
const data = await fs.readFile(JSON_FILE, 'utf-8');
todos = JSON.parse(data);
} catch (err) {
console.error('Error reading todos file:', err.message);
}
}
// Save todos to the JSON file
async function saveTodos() {
try {
await fs.writeFile(JSON_FILE, JSON.stringify(todos, null, 2));
} catch (err) {
console.error('Error writing todos file:', err.message);
}
}
// Get all todos
app.get('/todos', (req, res) => {
res.json(todos);
});
// Add a new todo
app.post('/todos', async (req, res) => {
const { text } = req.body;
const newTodo = {
id: todos.length + 1,
text,
done: false,
};
todos.push(newTodo);
await saveTodos();
res.json(newTodo);
});
// Update a todo
app.put('/todos/:id', async (req, res) => {
const { id } = req.params;
const { text, done } = req.body;
const todoIndex = todos.findIndex((todo) => todo.id == id);
if (todoIndex !== -1) {
todos[todoIndex] = {
...todos[todoIndex],
text: text || todos[todoIndex].text,
done: done !== undefined ? done : todos[todoIndex].done,
};
await saveTodos();
res.json(todos[todoIndex]);
} else {
res.status(404).json({ error: 'Todo not found' });
}
});
// Delete a todo
app.delete('/todos/:id', async (req, res) => {
const { id } = req.params;
todos = todos.filter((todo) => todo.id != id);
await saveTodos();
res.json({ message: 'Todo deleted successfully' });
});
// Load todos when the server starts
loadTodos().then(() => {
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
});