Lesson 11
Defining the DELETE Route to Destroy a Book
Summary
Summary of Getting Started with Express: Deleting a Book
In this lesson of "Getting Started with Express," the focus is on implementing the delete functionality for a book resource in the Express application.
Key Concepts Covered
-
HTTP DELETE Method:
- The DELETE method is used to remove a book by its ID.
- Endpoint format:
/books/:bookID
-
Implementation Steps:
- Use
app.delete()
to create the delete endpoint. - Parse the book ID from
req.params.bookID
. - Check if the book exists by using
Book.findOne()
:- If the book does not exist, return a 404 status with an appropriate message.
- If the book exists, call
Book.destroy(bookID)
:- If deletion is successful, respond with a 204 No Content status.
- If deletion fails, return a 500 Internal Server Error.
- Use
-
Code Details:
- Error handling for client errors (404) and server errors (500) is emphasized.
- The delete logic involves filtering the book list to remove the target book based on its ID.
Summary of Code Implementation
- The app structure accommodates the delete method.
- Test cases show successful deletion and error handling:
- Attempting to delete a non-existent book returns a 404 error.
- Deleting an existing book returns a 204 status, confirming the deletion.
In conclusion, the lesson reinforces the importance of managing different scenarios and responses in a web application, particularly in RESTful APIs. The instructor encourages practicing these error handling concepts as they are foundational for building robust applications.
This concludes the summarized lesson on deleting a book in Express. Looking forward to the next lesson!
Video Transcript
Welcome back to Getting Started with Express. So we did get all the books, a
book by ID, create a book, update a book. Now the only thing remaining is
deleting a book. That's what we're going to do in this lesson. So let's get
started. The HP verb is going to be delete. And then we're going to do slash
books slash book ID for the endpoint. Now delete is pretty easy because all you
need is the book ID and don't need a body. You're just going to find a book and
delete it. Okay, so let's go back to our app. And right after let's see food, right
after put right here, let's do app dot delete. Okay, and we're just going to
follow the same pattern as we did before. So I want to challenge you to try to do
the delete by yourself before we can move on. So how did it go? So let's try it.
We're gonna do slash books slash colon book ID, rec rest. And then we need to go
color book dot what we don't have it yet. So let's call it this try. So we need the
ID of the book to destroy and that's in rec dot programs dot book ID, but we need
to parse it. So let's do it. book ID. Let's parse int the rec dot per oops, rec dot
programs dot book ID. And use base 10. After that, we pass it to the destroy method that
so you have to be defined, that guy's going to remove it and that's pretty much it. Just
it should return actually, what should it return, maybe just the ID of the book that
was returned, something like that, because we what if we try to destroy something that
does not exist? Should this guy return no, I guess so. Actually, we should just first
find the book and to see if it exists. And then after that, we try to destroy it. So
destroy is just going to return the ID of the book that was destroyed if it succeeds,
otherwise it should do no. So let's just find the book first. We're going to do book dot
find one. The ID is book ID, constant book, like so remember, the fine one. Now, if I
want to find one and then I want to check if the book does not exist, then we don't need
to delete it, right? We should do a 404. How about that? We're going to say rest dot
send status 404. And just that same message we had before, let me copy it. Exactly, except
it's the book you want to delete does not exist. Okay, so do a return right here so
it does not go on to the next things we want to do. So we look for the book first, if it
does not exist return 404 otherwise we can proceed to destroy it because it exists, right?
And then after destroying it, we just want to check if destroyed book ID is that. So if
the book ID matched the destroyed book ID, something like this, so we can have a, we can
know it actually destroyed it, or you could just check if this guy is no actually. Yeah,
let's try that if destroyed book ID
is not no. And that means it actually destroyed it that we have defined yet,
we should raise rest dot send. We just send nothing actually, we can just the status 201
meaning empty, like success. And that's pretty much it. Let's do a send status.
We don't have to return anything if we delete it successfully just 201. Now what happens if you
forget to return here if the destroyed book fails, right? We don't have any
mechanism to check for errors right now because we're just doing the hard-booted
ORM like thing. So I would say just return some kind of error right here at the end in case
this does not match. So I know it's getting a little bit complicated and even though it's
supposed to be easy. Anyway, there's so error handle you have to think about it. I just want
you to get get started to thinking about how to handle these special cases. It makes a little bit
more complicated in any case. So if it happens so that the destroyed book ID, you didn't get
anything you got null because the operation to destroy failed, then you should definitely send
back a rest dot status or I don't know specific error you should be able to do right here. Maybe
let's do a 500 and you a general internal server error. And just like that,
a message could not destroy, oops, could not delete the book. I've liked that. Anyway, our case,
this is not not going to fail I hope because we're already checked the book exists and everything,
but it's just in case. Anyway, so let's define this book that destroyed right here. So down there
destroy in the book.js file, we need the ID of the book to destroy. All we have to do is go
through the list and eliminate the ones we find right. So we can go and use filter. It's a method
of the array prototype. So we filter out the guys that do not match the ID of the item we want to
delete. So we're going to do the following books, it's going to be equals to books about filter
book. So we're going to allow all the book whose IDs are is not the ID of the book to be destroyed.
This is going to filter allow every book to be in the new list except for the one
that matches the desired ID. Okay, so you're telling it please include the book as long as its ID is
not the one that I'm looking for. To do that or database is updated and we don't have to do anything,
we just return the ID back. Now if there somehow if we're working with an ORM or whatever,
in a given error, you would have to handle it here and return no sum to indicate there was a
problem or something anyway, we're not dealing with that. So going back to our app.js,
let's see what we have here. Let's test it out. Save this postman. Let's change the delete. Let's
delete book four. Well, the book you want to delete does not exist because we implemented that,
right? Now let's delete book number one. Created. Oops, 201 created. I think I've got the wrong code.
I think it should be 204, I'm sorry. Let me just do a quick search right here.
204 code. Yeah, that's it. I'm sorry. That was not 201. It was supposed to be 204, no content.
Okay, so let me fix that. I meant to be 204 if you successfully destroy a book because there's no
content, we just send the status code. In any case, let's get back to what we were doing.
Let's do a get all books. We have one, two and three. Remember the server restarted. So that's
why we have them. Let's delete the first one. I'm going to delete books one. I don't need a body
data right now. So don't need you ignore that. So delete the first one.
We got a 204 no content with nothing. Let's check the database. Okay, one is gone.
Let's do three.
Delete three, no content. Three is gone. Let's do want to to remove the last one.
Delete book slash two. Oops, what happened here? Oh, maybe there was a problem in your database
or not. Could not get any response. Oh, I don't know what I have. Anyway, let's check the
database. There's nothing now. Okay, so that's it. No problems here. We're all good.
Let's review what we did. This lesson we learned how to destroy or delete an existing resource,
or if it does not exist, we do 404. So first we have to parse the book ID from the rack
parameters, the route parameters. First we look, then we looked for the book to see if it actually
exists. If it does not exist, return a 404 status with a message error as the error,
error message. We all then we call booked up destroy of the book ID and everything goes well.
We know it's going to go well in our case because it was hard coded data without any promises or
async, async operations. And we if we got the ID, which we probably we're always going to get in
this case, if there's no error, we return a make a response with a 204 no content status.
Okay, and then we just return. And then we added just a 500 right here, in case an error would
occur after trying to destroy the book. In this case, it's not going to occur, because we're not
dealing with actual database, we're not doing async stuff and all that stuff. Okay, so it's
just hard coded. So I just wanted you to practice thinking about different cases where
you would have to return a different response, handle the errors, be it client errors or
the actual server side errors, that kind of stuff. Okay, so
I think we're good. And until the next lesson, bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: