Loading
Lesson 06
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Writing the Route to Get All Books

Video Transcript

In this lesson, we're going to get started into building our get slash books endpoint. This is the endpoint to get all the books in our fake database. If we go back all the way to the data structure we had before, we had a list of book objects. This is going to be our fake database. This course is focused on Express, so we're not going to deal with setting up and configuring the database. We just want to focus on the basics of Express. Let's make a fake database with hard coded objects, array of objects in the server side code. For the response for the get slash books, we're going to get this guy, 200k status code with this response right here. It's an array of objects with ID and title for each book. To do that, we have to build a fake API. We could directly send this from the send method and that's okay. But if you want something more elegant, let's create a module for our resource. Right here at NBK Books are the project folder. Let's create a book.js module. Let's do a module.expart, an object. And then let's define some methods. Let's define the method findAll that will be used to get all the books. So first let's do a hard coded data here. Let's copy that code. Assume this is our database, so we're going to copy this guy, an array of objects with each object have an ID and title property. If you want to take a moment to copy that. Going back to book.js, I'm going to make a cost variable here with the hard coded data right here. I'm going to say books. Like so. Actually, I'm going to make a let and I'm going to put lowercase in case we want to change it to make it permanently changed. Anyway, so we have this variable, leave it there. I'm going to fold it. And then we have in the module the findAll method that's going to return all the books. I'm going to assume it's not going to be asynchronous. So let's just do it synchronously without any kind of promise. So if you say findAll, we just want to return all the books. So that's just fine. So just return all the books as is. Now let's go back to, let's save this file and then go back to app. And then when you send this guy, we're going to do the following. We're going to call in the book module, findAll and this guy is going to return what? An array of objects. And that's going to be used for the send method. Now let's require a book right here. I'm going to go here, costBook is require dot slash book. You can put a .js, but I think you don't need it. So let's omit that. Save it. Now let's test it out. Go to your postman or whatever REST client you're using. One detail, if you forgot to change the route, it's a get slash books, right? So get slash books right here. Save it. Let's go back. Oops, postman. Let's say get slash books, the endpoint and let's send. So we got four and four not found. Oh, because we forgot to restart the server. So every time you make a change to the code, you have to go to the terminal, control C, and then the app.js. Now let's go to our postman, send. And then we got a response with all the books. Okay? 200 okay. So in this case, even if there were not any books, you would get an empty array. So we don't have to deal with four or four not found or any kind of error. Okay? Now going back to our code, that's the slash books route. We send some output from this module called books that we defined to be a fake database. We just hard coded the books, an array of objects, and we just returned those books and find all. Mind you that we're not using any kind of promises here. All right? Okay, that's great. So the next endpoint would be to get a specific boot by ID. We'll see you in the next lesson where we will learn just that. Okay?
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: