Hello everyone and welcome back.
In this video, we're going to be going
over how to create a post request and why we might need to use body parser
middleware to extract the data from the request body.
But first, let's understand what body parser is and why we need it exactly.
In node JS and Express, when a client sends a request
to the server, the request is received as a stream of data.
The data can be in various formats such as JSON, text, or binary.
The body parser middleware is used to extract the data from the request body
and make it available in a convenient format for us to use.
The body parser middleware parses
the incoming request body and extracts the data.
Without using body parser, we would have to manually parse
the request body ourselves, which can be time consuming and prone to errors.
There are different types of body parsers
available for different data formats such as JSON text or URL encoded data.
And in our case, we'll be using JSON body parser, which is used to parse JSON data.
So what we're going to have to do for this
is first we're going to have to install the body parser.
So for that, just go to your terminal,
make sure that you're in the root directory of your project where you have
server JS defined,
and run npm install body-parser
this will make body parser available
for you, and you can just import it from here.
And if you want to use body parser
with your app, all you have to do is type app use body parser JSON.
After this, body parser will be available
to you and we can start testing different request types.
So let's go over the first request type.
We have created a Get method before
which is used to request a resource from a server.
It's a read only operation, meaning it doesn't modify data on the server.
I need to start my server nodemon server JS.
Now my server is started on port 3000.
And then if I send this request, I'm going to receive hello world back.
So we could create a similar Get request and name this user.
And here we can write, you are getting a user data back.
And now if we send this request
and make it user, we're going to get this text back.
And if we were to use something else that does not exist, such as, let's say,
nata my name, you're going to get an error back because
we can't get the route Nata because we haven't set it up.
Okay, great.
So now that we understand how to use get
requests, let's talk about how to use post requests.
Post requests are used to submit data.
It's going to submit a data to a server to create or update a resource.
It's a write operation, meaning that it can modify data on the server.
So let's create a post request to create a post request.
All you do is app post and you can use any route that you want.
Let's create one on the user here as well.
Let's get the request parameters and respond with something.
If we want to see what we send in as parameters what we
could do is just console log, request body]
and to send in parameters using a post
request we can use postman so we can make
this a post request and set this to user.
And then we can just go to the body of this select row data.
And here we could just select JSON because that's the format that we chose.
And here we can just write some JSON data
such as Username for example, and it's going to be Nata.
And if this is JSON,
actually we need to put this as a string so that it's a viable JSON data.
And now if I click on send
here in my terminal, you're going to see username Nata printed
out because that's the parameter that we sent.
If you want to send more data, you're most welcome to.
You can say that first name for example,
is Nata and the last name is Vacheishvili.
And then if you send this again, you're going to receive the updated data,
which means that your server, when you make a post request,
would receive this data and it can use this data for anything such as updating
database fields and making sure that new user Nata exists in the database.
But we're not going to be going over how to update the databases in this section.
If you're more interested in serverside
development, you can take another course on it so that you get more familiar
with more complex functionalities that server side development can offer to you.
Now you see here that this is still sending the request and this is still
sending the request because we haven't responded with anything.
So if we want to respond with something, then we just can use response send here
and we can say that we created a user with
first name of request body first name.
Now let's save this,
let's go here and send this again and we're getting some kind of an error.
Let's see why we're getting this error.
Oh, because I put a comma here instead of plus.
So let's save this again and send this request.
And here you're going to see that we created a user with first name of Nata.
If I change this and make it Nicolas
for example, and send it, you're going to see Nicolas appear here,
which means that server is responding with the data that you sent to it.
So this is how easy it is to make a post request.
Post request, put request and delete request are very similar to each other.
So I'm just going to duplicate this code and call this Delete
and I'm going to duplicate this and call this Put as well.
And here we're going to say that we
updated a user with the first name of whatever we sent in.
And here we're going to say that we
deleted a user with first name of the given values.
Great.
So we don't need this console logs, or maybe we do.
Why not? You're going to see them appear here.
And then here we can just change the type
and say that we're sending a delete request and send it.
And here you're going to receive
an appropriate message because server will understand what type of request you're
making and make sure that it runs the appropriate function.
So here it runs we deleted a user with first name of Nicolas.
And here we can also send a put request because we set it up.
And if I send this, it will say that we updated the value.
And here we also see the console logs
from these two functions that we just ran on our servers.
Great. So now we understand how to make a get
request, how to make a post request, how to make delete and put requests as
well, and how to send and test the data using postman.
Now I also want to discuss one edge case.
As you see here, we have used the same
route with different request types and it works perfectly fine.
But if I were to use the same route
with the same request type, such as user get, for example,
the first instance of this is going to run and this is never going to run.
So let's console log,
this is never going to run here and we can send something different back here.
You cannot get this response back. Great,
so let's save this.
Our server is rerunning because it detected the changes and now we can just
send in the get request for the user and you're going to get back.
You are getting a user data back and that is run here because it only
recognizes the one instance if you have duplicated routes.
So make sure that you never create
duplicated routes because otherwise some part of your code might never run.
So that's all for this video.
Thanks so much for watching.
I'll see you in the next one.