-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
36 lines (30 loc) · 791 Bytes
/
app.js
File metadata and controls
36 lines (30 loc) · 791 Bytes
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
import express from 'express';
import { Todo } from './controller.js';
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/todos', async (req, res) => {
const todos = await new Todo().getTodos();
res.json(todos);
});
app.get('/todos/:id', async (req, res) => {
try {
const todo = await new Todo().getTodoById(req.params.id);
res.json(todo);
} catch (error) {
res.json({ msg: error });
}
});
app.delete('/todos/:id', async (req, res) => {
try {
const isDeleted = await new Todo().deleteById(req.params.id);
if (!isDeleted) {
res.json({ msg: 'Todo not found' });
}
res.json({ msg: 'Todo deleted successfully' });
} catch (error) {
res.json({ msg: error });
}
});
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}!`);
});