This course will be retired on August 10, 2021. We recommend "REST APIs with Express" for up-to-date content.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Use JSON, a widely supported and lightweight data format, to interpret data requests in the RESTful API.
So far, we have an Express app that runs,
but can't do much more than that.
0:00
To accept JSON data
coming into our routes,
0:04
we'll need to introduce some
middleware to our Express application.
0:06
While we could write the logic to
parse the JSON in the incoming request
0:11
ourselves, someone has already
written a module that will do this for
0:15
us called body-parser.
0:19
Let's install back here in the terminal
and save it to our package.json file.
0:21
npm install --save
0:28
body-parser@~1.15 This will save
0:32
this specific version to our package.json
file with the latest patch release.
0:42
To use this module,
0:47
we'll switch back to the app.js file and
0:52
enter, var jsonParser
1:00
= require("body-parser").json
1:05
Underneath the Express dependency
body-parser contains a number of
1:18
parsers to help you manage HTTP requests.
1:22
But we only need the JSON parser for
this project.
1:26
jsonParser is a function that will return
middleware that we can add to our app.
1:32
On the app object,
we can call the use method and
1:45
use jsonParser's returned middleware.
1:48
When the app receives a request,
this middleware will parse the request
1:58
body as JSON and make it accessible
from the request's body property.
2:03
This is similar to how we passed in the my
message property between middleware.
2:14
Let's take a look at this in action
to see it a little more clearly.
2:20
First, we're going to write some
middleware before the jsonParser is added.
2:24
This checks to see if the request body
object has the property named body.
2:37
If it does.
2:52
It logs out, the sky is, and
2:59
then the color from the incoming JSON.
3:03
If it doesn't, let's log out.
3:23
There is no body property on the request.
3:35
We're expecting that it doesn't,
3:42
because the jsonParser hasn't been able
to get his hands on the request yet.
3:45
Let's add the exact same middleware after
the JSON parser is introduced to see if
3:57
there's a difference.
4:02
This will work, but
let's refactor it slightly.
4:05
We've just added the exact same lines
of code twice in our application,
4:08
which is a red flag that we
could do something better.
4:12
Let's assign our middleware
function to the variable jsonCheck.
4:17
Then we can use it with our before and
after checks like this.
4:47
That looks so much cleaner.
4:58
Remember to save the application.
5:01
Move over to the terminal and
start the server.
5:04
In Postman,
let's make a POST request to our API.
5:11
Choose POST from the dropdown.
5:17
Make sure the url is localhost:3000.
5:20
Click on Body.
5:26
This is where we're going to put
the JSON in the body of our request.
5:28
Select raw and make sure that JSON,
application/json,
5:32
Is selected here in
the Content-Type dropdown.
5:38
In this text area here,
we can enter the JSON we want to send.
5:43
Let's put color: blue.
5:49
Hit Send, and we'll see the same error we
did with GET requests we made earlier.
5:57
But that's okay,
we haven't defined any routes yet.
6:05
Let's go back to the terminal and
see what we have there.
6:09
When the first middleware is hit,
the JSON is not parsed yet, but
6:15
the second shows that we
had access to the data.
6:20
This is after the JSON parser
was introduced to the app.
6:23
Great.
6:26
Let's stop the server and
delete the middleware we just created.
6:28
Leaving only the JSON parser behind.
6:39
In the next video, we'll start
building the routes for the API.
6:43
You need to sign up for Treehouse in order to download course files.
Sign up