-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (43 loc) · 1.11 KB
/
index.js
File metadata and controls
55 lines (43 loc) · 1.11 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
import express from 'express';
const app = express();
const PORT = 5000;
app.get('/', (req, res) => {
res.json({
message: 'Hello from DSC-IIEST',
});
});
app.use(express.json());
const todos = [];
// Todo Object Structure
// {
// id: string,
// task: string
// }
// GET: Receive all the todos
app.get('/todos', (req, res) => {
res.status(200).json(todos);
});
// POST: Create a todo
app.post('/todo/new', (req, res) => {
const { body } = req;
todos.push(body);
res.status(201).json(body);
});
// GET id: Receive the specific todo
app.get('/todo/:id', (req, res) => {
const { id } = req.params;
const todo = todos.find((val) => val.id === id);
res.status(200).json(todo);
});
// PUT: to update a specific todo
app.put('/todo/update', (req, res) => {
// Write the code to update the todo whose id is present in teh body
});
// DELETE: to delete a specific todo
app.delete('/todo/:id', (req, res) => {
// Write the code to delete the specific todo
});
const callback = () => {
console.log(`Server started on http://localhost:${PORT}`);
};
app.listen(PORT, callback);