1 00:00:00,000 --> 00:00:04,326 In JavaScript, we can handle events using callback functions. 2 00:00:04,326 --> 00:00:10,515 There are two types of events that occur, user events and system events. 3 00:00:10,515 --> 00:00:13,238 User events are triggered by user input, or 4 00:00:13,238 --> 00:00:16,042 system events are triggered by the machine. 5 00:00:16,042 --> 00:00:19,880 Examples of user events are mouse clicks, hovering and scrolling. 6 00:00:19,880 --> 00:00:23,825 An example of a system event is the set timeout method. 7 00:00:24,889 --> 00:00:28,966 The callback function supplied will run after a given time. 8 00:00:28,966 --> 00:00:32,678 The developer may define the time when the callback is executed, but 9 00:00:32,678 --> 00:00:34,411 the system is what triggers it. 10 00:00:35,732 --> 00:00:37,285 When we make requests in Node, 11 00:00:37,285 --> 00:00:41,181 different types of system events occur throughout the cycle of the request. 12 00:00:42,386 --> 00:00:45,983 These include data events, when data is received. 13 00:00:45,983 --> 00:00:48,804 Completion events, when a request is finished. 14 00:00:48,804 --> 00:00:53,560 And error events, and when an error occurs our first step in handling 15 00:00:53,560 --> 00:00:56,436 the data in our app will be to retrieve it. 16 00:00:56,436 --> 00:01:01,822 Or watch for data and run a callback to handle the data once it's received. 17 00:01:01,822 --> 00:01:05,489 To do this, we need to connect to the Treehouse API. 18 00:01:05,489 --> 00:01:10,174 Let's go to the Node docs and check out the https module. 19 00:01:12,299 --> 00:01:16,627 Go to node.org, docs, API reference 20 00:01:16,627 --> 00:01:21,957 documentation, find https. 21 00:01:23,330 --> 00:01:30,374 And get method, we're using https because we want to connect to a secure site. 22 00:01:30,374 --> 00:01:34,660 The get method takes a string of the URL we want to connect to and 23 00:01:34,660 --> 00:01:39,122 a callback function that lets us work with the response object. 24 00:01:40,269 --> 00:01:43,904 Let's create a folder called profileSearch to house this project. 25 00:01:45,458 --> 00:01:49,031 I'll use the mkdir command followed by profileSearch, 26 00:01:53,522 --> 00:01:55,349 And change into that directory. 27 00:01:59,397 --> 00:02:01,251 And now I'll open my text editor 28 00:02:06,895 --> 00:02:09,914 Inside of this folder, I create a file named app.js. 29 00:02:11,944 --> 00:02:14,404 That we'll later execute using node in the terminal. 30 00:02:17,301 --> 00:02:21,601 I'll paste some comments in here that will help us complete the app, 31 00:02:23,225 --> 00:02:24,629 Remind us of our plan. 32 00:02:33,916 --> 00:02:37,718 One thing that may be new, in order to use a module like https, 33 00:02:37,718 --> 00:02:40,586 we need to indicate that our code requires it. 34 00:02:44,202 --> 00:02:48,810 In the docs there's a variable defined as require ('https'). 35 00:02:50,700 --> 00:02:55,201 The require function in JavaScript allows us to include modules that exist in 36 00:02:55,201 --> 00:02:56,248 separate files. 37 00:02:57,527 --> 00:03:02,513 At the top, we'll store https in a variable named 38 00:03:02,513 --> 00:03:07,363 https with the require method. 39 00:03:09,246 --> 00:03:15,640 And https as a string. We will continue 40 00:03:15,640 --> 00:03:20,696 by creating a function to hold our data retrieving code and defining a request. 41 00:03:20,696 --> 00:03:22,496 Let's name the function getProfile. 42 00:03:39,051 --> 00:03:41,638 Next, I'll create a variable to store the request. 43 00:03:48,430 --> 00:03:52,743 And use the https.get method, 44 00:03:55,313 --> 00:04:00,044 With the string of the URL we went to connect to, the Treehouse URL. 45 00:04:05,866 --> 00:04:08,270 Now follow that with a callback function. 46 00:04:11,683 --> 00:04:13,676 That takes the response object. 47 00:04:17,019 --> 00:04:17,747 There we go. 48 00:04:20,889 --> 00:04:24,924 Node comes with a console.dir method that's similar to console log. 49 00:04:24,924 --> 00:04:28,706 When console.dir is given an object as an argument, 50 00:04:28,706 --> 00:04:32,245 it displays that object's properties in a list. 51 00:04:33,304 --> 00:04:37,617 Let's use console.dir to log out the response. 52 00:04:37,617 --> 00:04:42,038 Then in the console, we'll run our file with node app.js. 53 00:04:44,326 --> 00:04:51,822 We'll add a call to our getProfile function to make sure it's executed, save. 54 00:04:58,359 --> 00:05:00,458 Now run Node app.js. 55 00:05:02,373 --> 00:05:06,526 The terminal prints out a long list of properties from the response object. 56 00:05:06,526 --> 00:05:08,304 Remember that restaurant I mentioned earlier? 57 00:05:08,304 --> 00:05:13,275 Our current response object feels like the kitchen cooked the entire menu and 58 00:05:13,275 --> 00:05:14,890 brought it to our table. 59 00:05:16,131 --> 00:05:19,157 We weren't too specific about the response we wanted. 60 00:05:19,157 --> 00:05:21,170 So they brought out a bit of everything. 61 00:05:21,170 --> 00:05:24,810 One of these properties is the statusMessage of OK, 62 00:05:24,810 --> 00:05:27,634 which indicates the request went well. 63 00:05:28,839 --> 00:05:32,888 We get more detail about the status of our request from the statusCode. 64 00:05:32,888 --> 00:05:38,690 Our statusCode is 200, which indicates a successful connection to the API. 65 00:05:40,043 --> 00:05:44,779 Let's try selecting or accessing a single piece of this response. 66 00:05:44,779 --> 00:05:47,727 We'll use dot notation to access the statusCode property of 67 00:05:47,727 --> 00:05:48,837 the response object. 68 00:06:01,403 --> 00:06:05,808 Rerunning the application shows again that the statusCode is 200, which 69 00:06:05,808 --> 00:06:10,500 means that the request was successful and we've connected to the API URL, nice! 70 00:06:10,500 --> 00:06:14,695 We can interact with objects returned from API's just like other objects and 71 00:06:14,695 --> 00:06:17,821 access specific properties like statusCode. 72 00:06:17,821 --> 00:06:22,337 Great work so far, you've successfully requested data from an API and 73 00:06:22,337 --> 00:06:23,247 received it. 74 00:06:23,247 --> 00:06:27,519 Next, we'll learn how to convert this data to a format we can work with.