-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex2.ts
More file actions
124 lines (100 loc) · 3.1 KB
/
Copy pathindex2.ts
File metadata and controls
124 lines (100 loc) · 3.1 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
123
import express, { Request, Response } from 'express';
import path from "node:path";
interface Todo {
id: number;
title: string;
completed: boolean;
}
const app = express();
const port = 3000;
app.use(express.json());
let todos: Todo[] = [];
let nextId = 1;
// GET /api/todo
app.get('/api/todo', (_req: Request, res: Response) => {
res.json(todos);
});
// Permet de servir des fichiers statiques (HTML, JS, CSS) dans le répertoire 'public'
app.use(express.static(path.join(__dirname, 'public')));
// Route pour servir le fichier HTML
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
// POST /api/todo
// @ts-ignore
app.post('/api/todo', (req: Request, res: Response) => {
const { title, completed } = req.body;
if (typeof title !== 'string' || typeof completed !== 'boolean') {
return res.status(400).json({ message: 'title (string) and completed (boolean) are required.' });
}
const newTodo: Todo = {
id: nextId++,
title,
completed
};
todos.push(newTodo);
res.status(201).json(newTodo);
});
// GET /api/todo/:id
// @ts-ignore
app.get('/api/todo/:id', (req: Request, res: Response) => {
const id = parseInt(req.params.id, 10);
const todo = todos.find(todo => todo.id === id);
if (!todo) {
return res.status(404).json({ message: 'Todo not found' });
}
res.json(todo);
});
// DELETE /api/todo/:id
app.delete('/api/todo/:id', (req: Request, res: Response) => {
const id = parseInt(req.params.id, 10);
todos = todos.filter(todo => todo.id !== id);
res.status(204).send();
});
// PUT /api/todo/:id
// @ts-ignore
app.put('/api/todo/:id', (req: Request, res: Response) => {
const id = parseInt(req.params.id, 10);
const { title, completed } = req.body;
const index = todos.findIndex(todo => todo.id === id);
if (index === -1) {
return res.status(404).json({ message: 'Todo not found' });
}
// Ne pas modifier l'id — on ne met à jour que title et completed
if (typeof title !== 'string' || typeof completed !== 'boolean') {
return res.status(400).json({ message: 'Invalid input: title and completed required' });
}
todos[index] = {
...todos[index], // conserve l'id
title,
completed
};
res.json(todos[index]);
});
// POST avec fetch()
const createTodo = async () => {
const todo = {
title: "Learning this",
completed: false
};
try {
const response = await fetch('http://localhost:3000/api/todo', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(todo)
});
if (!response.ok) {
throw new Error(`Erreur HTTP : ${response.status}`);
}
const data = await response.json();
console.log("✅ Todo créé :", data);
} catch (error) {
// @ts-ignore
console.error("❌ Erreur lors de la requête :", error.message);
}
};