Building the remove Todo API request

Deleting a Todo is equally simple! Much like accepting a post route, we have to accept a delete route and specify an id in the body. This will allow us to use a URL such as /api/todos/3, where 3 would be the id of the Todo we want to delete! We can access params in the URL via req.params.[the name of the param]. In the following case, the name of the param we specify is :id, which means the variable we can access is req.params.id!

From there, we just filter out the instances of id that don't match and call it a day! Remember that any params URLs are passed in as strings; that's why we do a quick parseInt() on the id before our filter function! As follows:

app.delete("/api/todos/:id", (req, res) => {
const todoId = parseInt(req.params.id);
res.json({ todos: todos.filter(t => t.id !== todoId) });
});

Then we'll run it in Postman and verify the results:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.105.124