All right.
Up next, let's see how we can accept Json data so our get request works as expected.
Beautiful.
But now we need to add our first middleware so we can accept data that eventually is going to be coming
from the front end.
So let's say if the user wants to log in, we'll send the Json from the front end when we want to create
the job and hopefully you see where I'm going with this.
And in order to do that, like I said, we need to install and set up our first middleware.
And the way we do that, we go with app dot use.
So this is going to be applied to all of the routes.
It's a built in middleware at this point, so we simply need to go with express dot and then Json and
then we need to invoke it and then we'll set up a separate request.
And I'm purposely providing the same URL.
So it's clear that when it comes to a request, it consists of two things the URL and the method.
And even though the URL is the same, it's our home route.
Essentially, since the method is different, it's a post one as far as server is concerned, those
are two separate requests, so let's try it out.
I'm going to navigate to a server and for now I'll close the sidebar here.
And first we want to set up that middleware.
So we go with app dot use.
Again, it's applied to all of the routes we go here with Express and we invoke the Json.
Awesome.
Then let's go with app dot and the method is going to be what post?
So we'll create a resource like I just mentioned.
Yes, the URL is going to be exactly the same.
It's going to be our home route.
Now when it comes to the route handler, the controller.
We have access to req and res.
And in this case I will log just so you can see how giant req object is.
So the request object, what we're looking for is the req dot body.
That's where the data is going to be located.
Since right now we have our middleware.
So res.json.
Then let's pass in the message here.
Let's say data.
Received.
And then let's also send data property back with a req.body, because we already know that.
The actual data is located in the req.body.
Then let's navigate back to our Thunder client.
And essentially in here you have a few options.
If you want to create a new request, just click over here on a new request and of course you can manually
copy this URL.
Now, eventually we'll set up the global variables so we don't have to do that.
But for now.
In order to set this post request.
Yes, we'll need to grab this URL.
So that's one option.
And the second one, you can simply duplicate this request and I'll say no, this is what I'm going
to do throughout the project.
So instead of spending, I don't know, ten minutes going over.
The options, which at the moment most likely won't make any sense Once we get to that point.
During the project, I'll say, Hey, listen, this is how you can do stuff in the Thunder client.
So in this case, I want to duplicate this request.
Why?
Well, because I want to reuse the URL.
So URL is correct.
What do we need to change?
Well, this is a post one.
This is a post method.
We want to create a resource and now we want to navigate to a body.
This is how we send something to keep in mind.
We need to use a proper Json syntax.
What does that mean?
We need to use double quotation marks.
So notice over here, since I don't have.
There are double quotation marks for the value.
I'll right away have the error.
So if you'll try to send with that read error in the bottom, it's not going to work.
And let me just go with message and I'm going to go with Hello World because why not?
Let's send an if everything is correct.
First of all, we'll see the giant object because we're logging the rec object.
And also as far as the response.
Check it out.
So we have message data received.
And then I have the actual data, which is my hello world message and.
Like I said, what we're looking for in the req object is the body property.
And in there the.
Actual data we're sending is going to be located after we set up the middleware.
Again, it's not going to work unless you add this middleware.
And with this in place, we are ready to move on to the next step.