-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (33 loc) · 850 Bytes
/
index.js
File metadata and controls
40 lines (33 loc) · 850 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
37
38
39
40
const express = require('express');
const app = express();
app.use(express.json());
let todos = [];
app.get('/', (req, res) => {
res.json(
{
hasError: false,
todos
});
});
app.post('/', (req, res) => {
const { todo } = req.body;
id = todos.length > 0 ? Math.max(...todos.map(t => t.id)) + 1 : 1;
todos.push({
id,
todo
});
res.json({ message: "POST request received" });
});
app.put('/:id',(req,res)=>{
const {id} = req.params;
const {todo} = req.body;
console.log("id :", id);
console.log("todo :", todo);
let todoItem = todos.find(t =>t.id == id);
console.log("todoItem :", todoItem);
if(todoItem) {
todoItem.todo = todo + " (updated)";
res.json({ message: `Todo with ID ${id} updated.` });
}
});
app.listen(3000);