Loading
Lesson 08
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Parsing Incoming Request Body in Express using Middleware

Video Transcript

In this lesson, we're going to learn how to make a book. So we already learned how to get all the books, how to get a specific book by ID. Now it's time for us to create a new book object. We're going to do the post slash book, books, endpoint, and for that, if you look at our data structure, we have a list of book objects in our fake database, hard-coded data. So each book has an ID and a title. So when you want to create a new book, all you have to do is provide a title. The ID will be set automatically and given back to whoever creates the book. So in this case, this request has the body. And this body here is going to have the title of the book. And our back-end or server is going to take this, insert into our list of books, and generate a new ID for that book. We're going to return back the book with an ID assigned to it. So let's get back to our code. We're at the JSN. Let me just fold this guy. So we did the get slash books. We did the get slash book, slash book ID. Now it's time for us to do the app.post. So if you call post on the app, that's going to give you a poster out. The path is slash books. We're going to have our usual rec and rest parameters for the arrow function here. Now we have a problem. How do we get the request body into our application here? To do that, we're going to have to use a express middleware. A middleware is like a middleman between the incoming request and your defined route. So it's going to be intercepted. And we're going to get that request body and parse it into a JSON object in JavaScript because the incoming data is in JSON format. It's just a string, right? So we have to JSON parse it into an actual JavaScript object so we can work with it. So to do that, typically before express 4.16, you would use a body parse a middleware. Now we can actually just do express as a built in JSON middleware. But just in case you want to use body parse, I'm just going to tell you how to do it quickly. So all you have to do is go to the terminal. Let me kill the server here. And you're going to have to install the package called body parse it like so. And then this is going to automatically save to my package JSON because node 8 or that it does that. If I look at package.json, we now have the body parse a middleware 118.13 for express. So the way to apply middleware in express is before all the routes here right after we do our express app, we're going to use the use method on the app. That's going to allow you to use a middleware. So all you have to do is you're going to have to use the body parse a middleware. So first let's let's require the middleware right at the top. Let's call it body parser and require body dash parser. And then finally here you just call body parser dot JSON and you call it the method. This will apply the middleware. So every time it goes to your server is going to go through body parser to see if it has to parse anything from JSON to an actual JavaScript object or list. And that's going to create a property called body in the rack object. So you're going to have this rack dot body property. So to illustrate it we're going to do a rest in the rack dot body. Okay, just send it back when you go do a post slash books. So this is where the body of the request is going to be because we're using body parser. So let's save it. If you don't use body parser by the way, you're going to get this guy. You're going to get undefined. Let's go to the postman application here. So let's change the verb to post and slash books. Now to send a body in this request to include a body you click on body here instead of form there let's use the raw option with the JSON instead of text. So we have application slash JSON. So create a JSON. So what do we need to create a new book and need a title. So create a new property called title. Let's call it the awesome book like so and then you click send. Now I forgot to start my server. I'm sorry. Let's use npx no known app.js again. Going back to the postman application send. So we got back the same thing we sent. So that's good. So rect.body actually contains the body of the request. So that's what we learned. And just so you can actually see that body parser is needed I am going to comment this line and see what we get. Back to postman send nothing comes as a response because the rect.body is undefined. Okay. So that's all now in Express 4.16 there's a new built in middle air to parse the JSON. So I'm going to comment it out. Comment out body parser and instead here I'm going to duplicate this line and we're going to just use the built in in case you don't want to include an external library external third party app module that is body parser. So you can just say express remember we required express dot JSON here like so and it's going to do pretty much the same thing for us. Let's try it. Go to postman send it and there you go. You got back the awesome book. Great. Now now that we learned how to parse the request body it's time for us to. Implement our actual creation of the data. So if you let's go to book.js and let's create a new method. So this new math is going to be called create and what creates going to do is simply create a new book and insert it into this this existing list of books. Mind you that we have to figure out the last ID so we can increment it so the next guy is going to have ID for right. So the create method needs to take what take the actual book object with the title. So let's add the book parameter. So books are going to contain something like this. So first what we have to do is simply push to this array but we have to add an ID to the new record. So let's just do a simple books dot push with an object. We're going to push an object that has ID and a title. A title is going to come from the actual parameter right now what's the ID we need to.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: