 Okay, we have created get and delete requests and now we go into the next step which is posting, creating new users. So far we have only worked with dummy data, data that I put into my code, now once we have the post request we can actually work with whatever the user is sending in. And post is a different situation, the same goes for put and patch. You need to look at the request body. So that's where the user puts all the data that he or she wants to get for example created and you need to parse that. And if we use JSON we require something that actually parses the body properly so it gives us the right format back. You can do that manually but it's a lot of work so we use a specific module that exists in Node that's called body parser that just makes it very easy to process the request body. So what the body parser does is just it reads the body, it converts it to JSON and it has all sorts of error handling if that is necessary so we don't need to do that. And the way that works in Express is fairly easy. You just include it using the require statement and then you tell Express to use it. You just do app.use body parser.json. So body parser actually has more options than just JSON but since we use JSON here we just say use this one. So now we can access the body and you'll see that it's fairly easy to do so. The next thing we need to do is actually validation. So we need to check is everything we need there and is the input correct. So if you remember our data we had username that's a string parameter we had h so if you want to create a new user you need to check is the username there is the h there and you also need to check is the h actually a correct value. So it has to be a number it has to be zero or larger let's not care about maximum values doesn't matter but it should definitely not be a negative number for example ID if we do a create request we do not provide the ID we handle it ourselves so in our case we actually have to make sure that the ID is unique if you use a database you get that for free. Later on then when we create a user a note sorry we have the name and we have the content both are strings again we need to check are they there. So that's what we have to do and the way to do that is and I'll just continue so I'm not looking into the validation but we just create a new user object username is the body attribute username so that's what we get from the body parser we can just do request dot body dot the attribute name so request body username request body h that's what we got from the user and then we take the next ID and here I have just implemented a small counter that every time you create a new user it counts up so that makes sure that we always have a unique user ID and once this is created I just push it into my user area and I sent back 201 created and I sent the new object back so this is what I have created. That's all it is let's look into the implementation here so here's my post request and you see there is a bit more stuff than in the slides that's because I have included the validation first I actually check whether everything is there so I do check is the body undefined has the user forgotten to send a request body if not is the username undefined is there username and if that's not the case then I also check the age is the age there everything is fine if any of these things is missing then I just sent 400 back I say username and age feels are required so I send an error back to the user saying your request is invalid if everything is fine then I go in and I create the user and this is now exactly what you have seen in it's more or less the same as you have seen in the on the slides I create a new user with the right username the right age and the next ID I added to my user array and I increase the ID now the other thing I have to do and that has to do with my internal data structure you see up here that I have for each user I have an array here with the user ID and all the notes and when I create a new user I have to initialize this so I have to say okay the next user is for example 11 so please create a new object user ID 11 and an empty notes array when I create the user there is no note so that's the second part you see here I just create this element with the right user ID and with an empty array and I add it to my notes array and then I increase my ID so next time I create a new user I again get a unique ID and I return the user so this is already a bit more complicated and actually I have not checked the age here so that's maybe something I should do as well I have to check if request dot body dot age converted to a number is not a number so that should not happen or it is a number but it's smaller than zero then I send back an array again 400 and I maybe complain age has to be a positive number and actually this is an interesting case because there's an error here in my code as I've mentioned in the first part you actually have to return here this one would work because it's if or else so this code would never be executed I can actually skip my return statement here it doesn't hurt to have it but here I definitely need to add it because if I do not do this then the code just continues the user gets an error the user gets told that the age has to be positive but the user is actually added anyway we can maybe test that just so that I show you the consequence of this problem so if I run this code now I can do a post request to users now if my body is empty I'll get an error username and age fields so I actually have to go into the body here I have to choose raw Jason and now I can create a new user username Grisha age 55 this works perfect you also see that if I add myself again I'll get another ID so the increment works if I forget any of the fields I get a complaint using them in age fields and now the interesting part is if I choose an age that is lower than zero I will get an error message but this code will still be executed so you'll see in the console that I'll get this error that I'm modifying the headers but actually when I do my next get request the user will be there anyway so we start off by actually doing a get so you'll see that the users have been added this seems to work so let's make a post request where I put myself to minus one years or something else it doesn't have to be a number now I sent this I get a 400 back age has to be positive and if we look at the code you see here the error that something is broken in maybe I get the line somewhere as well in line 50 so you see this is exactly the line here where it complains that this code is being executed and now the interesting thing is I have gotten 400 it should not have worked but if I do a get request you'll see that it has actually worked so this is the problem it's simply I'm forgetting the return request here the return statement so the code executes and it keeps executing now if I restart this it should have worked so if I do the same story again it complains and you see this time there is no error in my console and this time if I do a get request nothing has changed so now actually my my input validation has worked correctly so this is how the post request works and now you can probably figure out how the put request works it's very similar you check that all the right fields exist and remember put is a complete update so I need both the username and the age same as the post request once I have made sure that my parameters are correct I find the right user check is the ID correct and I simply replace the attributes and return the user so I have done a complete update of the user and of course I can provide the same username so technically you can do something like a partial update only change the age but the back end doesn't care it just overrides everything no matter what you put in there so that's what the put request does we should probably also include our age validation here so if the age is incorrect I complain that works and now we look at the patch request finally so a partial update and there are some differences here so the first difference you see is in my check I do not check that the username and the age exists I only check for one of them so if the username and the age are both not present then the request is wrong because I need at least something to update but if any of them is defined then this is okay and I continue into my for loop I find the user and then I update the fields and of course now I need to check whether the parameters are actually there because if I would just override it and the user has not sent anything then my attribute would become undefined so I basically say if the username is not undefined then update it if the age is not undefined update it otherwise leave it as it is and what you also see here is that I can actually use patch in exactly the same way as put so if I provide the username and the age it works exactly the same as the put request there's no difference so that's kind of the same this is what a partial update means of course if this gets a bit more complicated when you have more parameters there are more things to check but otherwise the principle is exactly the same if we look at a post request for nodes our URL is different of course the parameters are different because our nodes have a name and a content so of course I'm not checking for age and then the other difference is that I actually first have to find whether the user exists so I'm searching my nodes array for an object that has the correct user ID if that's the case I create a new node with the name with the content with the next ID so again I have some kind of ID counter for my nodes I add the new node to the right field so in my nodes array to the user nodes array I add the new object I increase the ID I return the node same story we can also look that that works just to make sure so let's have a look here are all my users and let's for example choose Bob if I say get users 10 nodes then I should get all the nodes and Bob seems to have a shopping list with milk and cheese now we can do a post request so we just post to this one and I can just try to send this then I should get an error name and content fields are required I don't have any of that so let's say name content and both are strings my new nodes pick up daughter from the day care I'm less down strike I do this post request now I did not get an error I get 201 I get the new object back and now if I do a get request I should get the right content so you see that I both get my shopping list and my new node you see that the order here is different that really doesn't matter so this does not matter in Jason so this works I can also do the get for a specific node so for example 13 so please get me node number 13 for user number 10 and this also works if I choose a different user ID here so node 13 does exist but not for the user 5 so this should give me an error and indeed it says 400 400 found node with ID 13 does not exist for user 5 so this also works okay so this was a single slide how to do post and put in patch as you see they are a bit more complicated and especially when when your parameters get more complicated this code might change on the other hand don't be don't be fooled delete requests for example can or also be very complicated if your relationships between the different data structures are complicated so these things can evolve to be quite tricky this is now the complete application I have in the example material I have actually implemented all of them so I'll upload that in the next step now we'll take this and deploy it to the cloud so that we can run it not only on our own machine but also somewhere else