Loading
Lesson 09
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Defining the POST Route to Create a Book

Video Transcript

We need to find out what the last ID is. We do not necessarily want to just take the last one in this list and add one because usually in databases, relational databases, if for example I delete this guy and you add a new record, the ID would actually be 4, not 3 because we deleted 3 and that's already used in the past. So we're going to keep track of the last ID like so. So the last ID is going to be 3 in this case because we have 3 records and the last ID that was used was 3, okay? And we're going to update this as we add or remove new records. Sorry, as we add. Anyway, so going back to what we have here, we're going to use 1, 2, 3. Last ID was 3 so we need to use 4. So we're going to first increment last ID then use it for the new ID. Okay? So let's make a variable here, const ID. First you're going to do last ID plus plus and then we do ID as last ID. There are many ways you could do this. You could even do like this, for example, this will first increment last ID then use its value. So that's going to make last ID 3, 4, 3 plus 1, 4 and then use 4 as the value for this ID. Okay? So we can use ID here because we can also use this shortcut if the property has the same name as the variable here, you can do this, okay? So we increment the last ID, got all that, sounds good. Now this looks good, we push but then what about the response? What are we going to respond with? Well, typically we would just reply with the actual object including the ID. So let's do that, let's extract this new book into a variable called new book. New book is just this object here, okay? And then we send the new book back to the client. Let's try it out, save this. Oops, actually we're not in the controller in the route yet, right? So I'm sorry, I went too far ahead. We're actually still inside books. So actually we want to return just the new book back to whoever called this, alright? Now this was called on the route, post route here. So we would have to send a new book like so, okay? So I got it later ahead of myself. This would be book.create with whatever and this would return the new book, right? So let's backtrack a little bit. We first need to get to call the book.create. We need to first parse the JSON, we already have that done using the middle error. Now we got to take that and pass it to book.create. So you can do just rec.body right here. And you're going to pass the whole body to the book.create method. And that's going to go there, going to increment the ID, generate the new book, push to the books list and return the new book. Go back here, take the new book and then we send. And that's it. Mind you that we did it very quick and in a simple line, typically you would have to fit like, what if the client tries to send more properties than you need in the rec.body? And that's not really nice. So typically you would pick the properties you want and it filter out all the unwanted ones. So you could do that here if you made a new object like, JSON book is a new object with the title being rec.body. You could use low dash with the pick method, you know, like so. This kind of thing. All right. So I'm just going to leave as is, because I don't really mind at this point we're just learning about keeping mind you should definitely do that in a real project. Okay, let's test it out. Go here to postman. Let me first get all the books so you can see what the list looks like. Oops, the server is not running because I crashed. Did I miss something? Unexpected identifier 31. Line 31 of book. Okay, I forgot the comma right here. So there's a comma. Sorry about that. Let's go on. So it was now running again. So postman, let's go back at a list of other books. One, two, and three. Now let's create a new book with post slash books. Let's go here to the body. We have a application JSON. Let's give it a title. The awesome book. Let's click send. So we got back the awesome book with an ID for great. Now let's get all the books again. I'm going to open a new tab and I'm just going to say this local is 3000 books. Just want to see if the actual list has been updated. So as you can see, it's now there. The fourth record, the awesome book. Let's create another one. Another awesome book. There you go. So we finished our post slash books route. So first we have to make sure to create the book in the database. We're using a fake one, by the way. You got the racked out body to get all the properties that you need to create the book. This case was just that. We just put that all in. I didn't filter out unwanted properties, but you should definitely do that. And we send it back to the user. Very simple. Okay. So for this lesson, that's it. And I'll see you next time. Bye.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: