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