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
In this video we'll get the body from the response using something called a Stream.
Documentation
We've got the status code printing out but
that isn't much good.
0:00
We need to dig into
the body of the response.
0:04
In order to get the body.
0:07
We need to read it in from the response.
0:09
Let's jump back into the documentation and
see how we can do that.
0:12
The response object has a data
event that gets emitted
0:16
when a piece of data comes in.
0:20
Let's try this out in our code and
0:23
log out what's happens when
this event is triggered.
0:25
Let's run it and see what we have.
0:49
You may be expecting the data
handler to run once after all
0:51
the data from the body comes in, but
it's not it's run several times.
0:56
With only fragments of the body.
1:05
It's also not a string.
1:12
It's a buffer, a common data type emitted
by the node network and file events.
1:14
When something is sent over a network
like the Internet, is not sent in one go.
1:20
It sent in packets of information,
many programming languages or frameworks,
1:26
wait until all data is transmitted
before you can do anything.
1:31
However node JS uses streams to
implement its non-blocking features.
1:35
So your application is free to do
other things, whilst is waiting for
1:42
more data to be transferred.
1:45
To convert a buffer into a string call
the toString method on the buffer.
1:47
We're not worried about buffers right now.
2:00
Just know that you can change them into
strings easily with a toString method.
2:03
We can construct the body of
the response by concatenating
2:08
each piece of data to
the end of a variable body.
2:12
Let's create a body variable,
which will have an empty string initially,
2:16
then we can add the chunks
of data as they come in.
2:21
But how do we know when it ends.
2:29
It's not clear on the documentation page.
2:31
But whenever you see
the data events in Node.js.
2:35
They'll be an end event.
2:40
When reading data off a hard disk or
2:42
off a network connection with node.JS
APIs, they all comply to the same pattern.
2:44
They emit a data event, and
a chunk of data comes in.
2:50
And then it emits an end event when
it's completed reading the data in.
2:54
Let's implement the end
handler now on the response.
3:00
Let's implement the end handler by
writing response on, and the end event.
3:05
Next our event handler or callback will
just print out the body using console.log.
3:12
Now that we have the full body,
how do you make it into something usable?
3:23
Right now the body is just
a string not an object.
3:35
If you're ever unsure of the type of
an object you can use the type of keyword.
3:39
When we run this, we see that's the body
of the response and then its type string.
3:55
Next we'll make this into
an object by parsing the string
4:03
You need to sign up for Treehouse in order to download course files.
Sign up