Loading
Lesson 07
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Writing the GET Route to Get a Specific Book By Id

Video Transcript

Now you're back. So let's write the get slash book slash book ID route. So this route is to get a specific book by ID. So our endpoint is going to have slash book slash the ID of the book. Could be one two three four whatever. So for that we're going to use route parameters. In Express route parameters are represented with a colon followed by the name you want to call the parameter. This case I just call book ID. So these guys colon book ID is going to appear in the rec object as a property of the rec dot prams object. Okay. So if you go here our app.js we already have the slash books. Let's make another route app.get route. What's the path slash book slash what colon book ID. This is a route parameter because there's a colon. This variable name will be will appear in the rec dot prams object. Load to find a rec. R-E-S arrow function. So I'm just gonna do a rest dot send. I'm gonna send back rec dot prams. So you can see it's actually what's inside this object right here. I'm gonna send back rec dot prams. That's where the route prams are. Let's save it. Go to postman. Oh mind you that we have to restart the server. You can kill this guy. Notice we keep restarting the server and that's very annoying. So there's actually a tool to not have to restart it and have it do automatically. It's called Noledmon. If you want to do it I'll teach you right now quickly. You can do npm install dash dash save dev. By the way I is if you say I that's like a shorthand for install and Noledmon is the module. This is gonna use save dev because this is just a tool for development. We don't want to have this package in the production. Build for the app okay. You don't need that. So it's gonna take some time. Great. Now to run Noledmon instead of running the app node app.js you're gonna use Noledmon. You're gonna Noledmon app.js. But if I do it like this it's gonna call upon the globally installed Noledmon. We don't have it. I don't have it so I'm gonna use the Noledmon that was installed as a dependency for our current project with npx prefix. Just before doing that go to the atom text editor package.json. You can see dev dependencies now has Noledmon version 1.18.4. So this is just for development. So we are node modules right here. If you notice there are many files. So we want to use the Noledmon from this node modules directory. To do that we use npx prefix with the Noledmon app.js. With that Noledmon is gonna be start watching the file app.js. And if you make any change for example if I go here I change the book. I add a comma or something. And I go back. You're gonna see it restarts the server after making a change. That's great. So now we don't have to restart the server every time we make a change. So that's Noledmon. Okay so let's keep going now. Back to where we were before. We wrote the endpoint slash books slash book ID. So let me try it out. I'm gonna put slash one for the ID. Send. And we got a 4.4 and a found. What's going on? Let's go back to our code. Save this app.js. We got app.get slash book. Oh it's forgot it. S right. The s right here. Save it. Go back to Noledmon. We are sorry go back postman. We already have Noledmon taking care of restarting the server. Try again. Now we got an object on the JSON right here with book ID one. So this guy this route parameter that we call book ID appears in the rack dot parameters object. That's how we get the route parameters. Okay so let's go back. So we know that rack parameters has dot book ID. So we're gonna use that to search for a book. But now we're gonna use our book module find one. Let's call a new function find one where you give the ID. Okay right here. And then it's gonna find by the ID. Mind you one thing before we continue. Rack dot parameters dot book ID is a string. Look at a postman response. This is not a number. It's a has quotes. So it's a string. So all route parameters are a string. If you need the number actual number type you need to parse it. But remember that always parse your parameters. So instead of doing this guy let's parse it. So I'm gonna say book ID is going to be parsed int with the rack parameters book ID and 10. We're gonna parse this into an integer because we assume the ID is an integer in our case. Okay so remember data structure uses an integer for the ID. So with that we're gonna use book ID right here already parsed as an integer. Mind you we're doing very simple things here. So I'm not assuming this might not give out an error in parsing it or the argument is given not as a number as a string. So there are all these special cases you have to handle. Let's ignore that just for the sake of focusing on learning express. Okay so be aware that book ID somebody might not write a number there or something else and then you have to handle that. Anyway so we assume you're gonna create the find one function that when you call it with the ID of the book it's gonna return the object the book object whose ID is book ID and whatever name it is. Okay but if you just send it like this right what if it doesn't find it it's gonna return no right. We'll get to that later so let's focus on writing find one. So I'm gonna save this and go to the book.js I'm gonna write find one right now and the the parameter for this method is going to be the ID the book ID. So to find a book by ID in this list is very simple you can use the find method of the array type right. So let's do that. So we're gonna return books.find and we're gonna find a book whose ID okay so we're gonna find a book whose ID is the ID given. We assume this is an integer okay must be a lumber type of integer to match these guys we're doing strict equality. So that's it if it doesn't find it it's gonna return no. So let's try it out save this go back to the app just see if things are okay great let's try it out in postman. Let's we know the book by ID one exists so we let's try it send it so we got it back right ID one title picture of door ingrate great that's awesome let's try number two. I booked two and book three and book four there's no book four so we got nothing because we return no so that's not very good to get a 200 okay of no so you want to usually if you don't find a record you want to say to the user 404 status code okay and usually some error message if you want. So going back to our route definition we got to think through this and we don't want to return no so let's do something else right here so let's put this guy in a variable called book let's say const book is book find one book now we have one condition here this guy sends the book assuming it exists but if the book does not exist what do you do if book if it's no this is going to be true so we execute something so we want to return a response with a status of 404 so we're going to send status 404 in this case let's see what we get by the way we should return right away otherwise it's going to call our yes again and that's not good so make sure to return after this early our yes send okay we don't want to send a book that's no so let's try this out since status is going to send a response back of status 404 usually used for not found things are not found so let's try it out going back to postman send again look we have a status not found now that's great now we didn't have any specific error message in JSON format if you want to add that let's try it go back to the text editor and you can concatenate here with a send an object I can put I don't know a message for example error message or description you can say could not find the book something like that let's try seems like it did not work let's think through this send status 404 send let me look just the moment I'm try something I put the 404 before the message I think this is already deprecated so but I want to see the error message in a console let me try to postman now we got to could not find a book but this is deprecated use our yes dot status status dot send okay so we should actually use so this is a good practice for us to deal with deprecated things so you read the express deprecated message so you should not do that don't use the rest send with the status quote on the body anymore should use separate things like our yes dot status with the status quote and send with the body okay so I just missed that I put send status here instead of just status going back to what we had before this should be just status okay our yes dot status 404 and then you send something save it try it again there you go 404 and the message could not find a book great nice so let's review what we did in this lesson we learned how to make the slash book slash book ID route we learned about route parameters if you put a call and follow some name it's gonna appear in the rack dot per am's object with that same property name notice that this is always a string so if you have a number there you have to parse it for example in our case parse the book ID into an it then we built a fine one fake database response which is if you give a book ID you get the it searches for that book ID if it doesn't find it make sure to return a response with status 404 and some message and make sure to return early from the route otherwise you're gonna go on and try to send something again that's not good I always remember the return and then if it finds the book this condition we follow so it's not gonna execute anything here but it's gonna send the actual book object as found okay so that's it for this lesson and I see you on the next one
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: