You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constexpress=require("express");constapp=express();constbodyParser=require("body-parser");consttodoList=[{id: 1,task: "learn Express",done: false},{id: 2,task: "learn Express-Generator",done: false}];// parse application/x-www-form-urlencodedapp.use(bodyParser.urlencoded({extended: false}));// parse application/jsonapp.use(bodyParser.json());//get all todo listapp.get("/",(req,res)=>{res.send(todoList);});app.get("/:id",(req,res)=>{try{constfilteredTodo=todoList.find(item=>item.id==req.params.id);res.send({message: "Here is what you looking for",
filteredTodo
});}catch(error){res.send(error);}});// add new todoapp.post("/",(req,res)=>{try{letnewId=todoList.length+1;letnewTodo={id: newId,task: req.body.task,done: false};todoList.push(newTodo);res.status(200).send({message: "todo successfully added",
todoList
});}catch(error){res.send(error);}});// ~delete todo by its id~app.delete("/:id",(req,res)=>{try{constidToDelete=req.params.id;letnewTodo=todoList.filter(item=>item.id!==parseInt(idToDelete));todoList=newTodo;res.status(200).send(todoList);}catch(error){console.log(error);res.send(error);}});// ~update a todo by its id~app.put("/:id",(req,res)=>{try{letgetTodoToUpdate=todoList.findIndex(data=>data.id==req.params.id);todoList.map(data=>{if(data.id==req.params.id){todoList[getTodoToUpdate].task=req.body.task;}});res.send({message: "data successfully updated",
todoList
});}catch(error){res.send(error);}});app.listen(process.env.PORT||3000,()=>{console.log("Your server is running on PORT "+process.env.PORT);});
The text was updated successfully, but these errors were encountered: