In this video, we are going to learn how we can pass a data to our server because a server can also
take a data from a user process it and gives back the response.
But to make it pretty easy, we are just going to learn how not just server can pass incoming data and
then display it to the console.
Remember, a server can take in a data and then send back the response.
So let's see.
So what you are going to do is that we are going to mimic in such a way that we are going to create
a post with a title and then description.
And with that data we want to receive it on our server.
So let's see how.
So let's collapse this once the one that we done with it and now let's open the server file.
So now definitely we are also going to listen to an end point.
So come down here after register.
You see that pass?
Incoming.
Data.
And we often say payload, meaning the data that a user is trying to send to our server.
So here we are going to check for a post route or post endpoint.
So the next step is how can we get the data?
The first step is let's check the you are the user is trying to hit for our case.
Let's say create post is going to be that.
And guys, we have a couple of ways that we can send data to a server and then also get data from a
server.
And this is what we call a HTTP method.
We have dedicated video and HTTP methods.
But for now, any time you want to send data to a server, we use HTTP method called post.
So on the request, we have all the HTTP method.
For the meantime, let me comment this one out and let me show you what I mean here.
So inside the home root here or any of this root or endpoint, let's try to console log the request,
this request.
But last we can do it here on the request we have for this call the method.
So request that method.
Now when I try to visit any of these endpoints, this console log will be triggered.
So let's see.
So let's restart the server.
And Clay the console and here we go.
So now let's go to any end points.
Check it out.
In the console, you can see that we have this HTTP method called get meaning.
We are getting something from the server and if we want to send something to the server, we are going
to use what is called post method.
So now let's remove this one or I can leave it for now and now let's bring back this code.
The first step is we are checking if the you are o is equal to create post and the request dot method.
Is equal to post.
Uppercase post.
Then.
It means that a user is trying to send something to us.
So here let's receive the incoming data.
So here receive.
Receive the incoming.
Data.
Perfect.
So let's assign to a variable as post and let's give it an empty array.
This is how we pass data.
Insight.
Chase The next step is that we are going to listen on an event called Data.
So on the request, remember, which is from this one.
This one.
We also have an event listener dot on.
So you're going to listen to an event which is of type data, meaning if a user is trying to send us
data, this event is going to be fired.
So let's pass in callback function here.
That is a function.
And inside this function we have what is called the chunk.
When we say chunk, it simply means that when we send a data to our server, it comes in piece by piece.
Then we will compile it and then push it inside this post array.
As you see over here.
So inside here we have the actual payload as chunk.
So here, if you try to console log chunk.
Now let's see.
So the chunk represents the pieces of data that we are receiving from the user.
So now how can we make post request?
How can we create?
Well, we are going to use different tools to get it done because for backend development, we don't
have a browser to enter data like maybe the title of the post the description.
So we need a two.
So we are going to use what is called tender client.
And this is an extension that applies to make a request, whether post or get or PWD or whatever.
Like I said, we have a video on this.
For the meantime, let's see how we can send request to a server.
So if you don't have tender client set for tender client hit enter and this is the extension tender
client.
So install it after installation, you're going to see something like this.
So now here you go, open.
That next step is click on new requests.
The request has opened here.
For the meantime, don't worry about what is going on here.
The first step is because we want to make post request.
Let's change this one to post again.
Don't worry if you're not okay with this.
We have a section talk about the two and we are going to use what is called postman instead of this
one.
So I just want to show you how we can receive data inside our server.
So here you provide the you are going to be at HTTP semicolon, first left slash look host semicolon
9004 slash create post you saw it as that and to be able to send data in the past in what is called
body.
Click on body here and inside the JSON content, I'm going to send what JSON data?
Remember, we always say JSON data to a server for this.
We can send any data here.
So let's say title are going to be the property name and let's say best post and comma and description
is that let's say that great post ever.
Now when I hit send, you can see that something is happening here.
Don't worry about this one because we are not sending anything to the user.
So let's check the console.
If I going to receive the data, which is the data that we sent and we are seeing nothing here is because
we also need to listen to an event called End.
So here to be able to receive the actual data that is from this on here we are going to change another
event listener call on and the type is what you call.
And at this point the process in the data has been passed from chunk to extra data and we have access
inside this end.
Event Listener So let's pass in normal function here it can be error function.
And here if we try to console log let's say that anything data.
Here.
Okay, so now let's restart the server.
Click the console, and here we go.
And you could see that connection was forcibly closed by a player, meaning nothing sent to the user.
So.
So let's try it to send again.
Hit send.
Now check the console.
And now you can see that we have data here and the chunk we see something like bar file and this is
how Node.js parses incoming data.
So a buffer simply means that the data is being passed bit by bit.
So after everything is complete, then convert bar to actual data.
So here what we are going to do is that now our survival hang up because inside here, inside here,
this function call here we are not sending anything to the user or we are ending the the response.
That's why the server is still hanging.
So now inside here we are going to push the post dot push and now you push in the chunk.
So inside the end here we are going to pass the buffer to actual data.
So here pass the.
Bar from data into string or actual data.
Okay, so here we go.
So Konst passed.
Data is equal to the buffer or is a method on that.
We have what is called contact and then we can cut what we can cut the post which is here and you pass
passing here.
And lastly, we need to convert to string and that is it.
The next step is we need to send the data to the user.
Right?
So let's send the header rest dot right head.
Right head, then it passed in 200.
That is okay.
And now the content type for this one.
Should we pass in JSON data or object?
So here content.
Type.
Again.
Let's just send.
A type of atmo or adjacent.
Don't worry.
For now.
Okay.
Think.
Yeah.
Close your eyes.
That that is the next step is let's try to console.log the actual data that is past.
Past data and now let's end or let's send something sets that red dot right and I'm going to say that
post created and let's end a response here as red dot and now we are done with our implementation.
So let's shut down a server again, clear the console and now clear the console and now let's restart
it.
And now check it out.
Sorry.
Inside here you see that the server has hangar because at first we're not returning anything to the
user.
So now let's try again sent and now we get a response called post created.
And if you check the console, we see that we have received the data us that guys this is crazy.
I really like it.
Well, if we are using Express, I'm going to write a few lines of code to get this job done.
The next video.
Let's continue.