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
We'll use one of Node.js' built-in modules `https` to go out and make a GET request.
API URL
Due to a recent change in the routing of our website the path as shown in the video is no longer working. You'll want to change the URL to be:
// Connect to the API URL
https://teamtreehouse.com/profiles/csalgado.json
Documentation
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In JavaScript, we can handle
events using callback functions.
0:00
There are two types of events that occur,
user events and system events.
0:04
User events are triggered by user input,
or
0:10
system events are triggered
by the machine.
0:13
Examples of user events are mouse clicks,
hovering and scrolling.
0:16
An example of a system event
is the set timeout method.
0:19
The callback function supplied
will run after a given time.
0:24
The developer may define the time
when the callback is executed, but
0:28
the system is what triggers it.
0:32
When we make requests in Node,
0:35
different types of system events occur
throughout the cycle of the request.
0:37
These include data events,
when data is received.
0:42
Completion events,
when a request is finished.
0:45
And error events, and when an error
occurs our first step in handling
0:48
the data in our app
will be to retrieve it.
0:53
Or watch for data and run a callback
to handle the data once it's received.
0:56
To do this,
we need to connect to the Treehouse API.
1:01
Let's go to the Node docs and
check out the https module.
1:05
Go to node.org, docs, API reference
1:12
documentation, find https.
1:16
And get method, we're using https because
we want to connect to a secure site.
1:23
The get method takes a string of
the URL we want to connect to and
1:30
a callback function that lets us
work with the response object.
1:34
Let's create a folder called profileSearch
to house this project.
1:40
I'll use the mkdir command
followed by profileSearch,
1:45
And change into that directory.
1:53
And now I'll open my text editor
1:59
Inside of this folder,
I create a file named app.js.
2:06
That we'll later execute
using node in the terminal.
2:11
I'll paste some comments in here
that will help us complete the app,
2:17
Remind us of our plan.
2:23
One thing that may be new,
in order to use a module like https,
2:33
we need to indicate that
our code requires it.
2:37
In the docs there's a variable
defined as require ('https').
2:44
The require function in JavaScript allows
us to include modules that exist in
2:50
separate files.
2:55
At the top,
we'll store https in a variable named
2:57
https with the require method.
3:02
And https as a string. We will continue
3:09
by creating a function to hold our data
retrieving code and defining a request.
3:15
Let's name the function getProfile.
3:20
Next, I'll create a variable
to store the request.
3:39
And use the https.get method,
3:48
With the string of the URL we went
to connect to, the Treehouse URL.
3:55
Now follow that with a callback function.
4:05
That takes the response object.
4:11
There we go.
4:17
Node comes with a console.dir method
that's similar to console log.
4:20
When console.dir is given
an object as an argument,
4:24
it displays that object's
properties in a list.
4:28
Let's use console.dir to
log out the response.
4:33
Then in the console,
we'll run our file with node app.js.
4:37
We'll add a call to our getProfile
function to make sure it's executed, save.
4:44
Now run Node app.js.
4:58
The terminal prints out a long list of
properties from the response object.
5:02
Remember that restaurant
I mentioned earlier?
5:06
Our current response object feels like
the kitchen cooked the entire menu and
5:08
brought it to our table.
5:13
We weren't too specific about
the response we wanted.
5:16
So they brought out a bit of everything.
5:19
One of these properties is
the statusMessage of OK,
5:21
which indicates the request went well.
5:24
We get more detail about the status
of our request from the statusCode.
5:28
Our statusCode is 200, which indicates
a successful connection to the API.
5:32
Let's try selecting or
accessing a single piece of this response.
5:40
We'll use dot notation to access
the statusCode property of
5:44
the response object.
5:47
Rerunning the application shows again
that the statusCode is 200, which
6:01
means that the request was successful and
we've connected to the API URL, nice!
6:05
We can interact with objects returned
from API's just like other objects and
6:10
access specific properties
like statusCode.
6:14
Great work so far, you've successfully
requested data from an API and
6:17
received it.
6:22
Next, we'll learn how to convert this
data to a format we can work with.
6:23
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up