1 00:00:00,440 --> 00:00:04,150 Being a developer in any environment requires you to understand the tools we 2 00:00:04,150 --> 00:00:07,040 have for building any given application. 3 00:00:07,040 --> 00:00:11,710 What do we do when we get stuck, where can we look for possible APIs that we can use? 4 00:00:11,710 --> 00:00:13,630 Here's a few tips. 5 00:00:13,630 --> 00:00:17,630 If you get stuck in this course, or even in your own projects, you can ask for 6 00:00:17,630 --> 00:00:20,100 help in the Treehouse Community Forum. 7 00:00:20,100 --> 00:00:22,910 Your fellow students can give you hints and suggestions. 8 00:00:22,910 --> 00:00:27,110 In fact, students are rewarded for participating in the community, and 9 00:00:27,110 --> 00:00:29,090 by giving the best answers. 10 00:00:29,090 --> 00:00:33,920 Be sure to remember to embed your code, or link to the source code of your project. 11 00:00:33,920 --> 00:00:36,940 Got an error, and not having much luck in the community? 12 00:00:36,940 --> 00:00:39,950 There's a chance someone else has had the same issue as you. 13 00:00:39,950 --> 00:00:41,300 How do you find them? 14 00:00:41,300 --> 00:00:43,160 Open up your favorite search engine, and 15 00:00:43,160 --> 00:00:46,230 paste in the most relevant part of the error message. 16 00:00:46,230 --> 00:00:48,590 There's countless numbers of blog post, sites, 17 00:00:48,590 --> 00:00:53,500 forums, newsgroups, where people post their problems, and how they solve them. 18 00:00:53,500 --> 00:00:57,950 But probably the most important place you can get see what's possible with any given 19 00:00:57,950 --> 00:01:02,770 language, framework, or platform is, by going to its official documentation. 20 00:01:02,770 --> 00:01:06,480 In our case, it will be the node.js documentation site. 21 00:01:06,480 --> 00:01:09,186 Let's take a quick look now. 22 00:01:09,186 --> 00:01:13,479 Node.js is official website is, node.js.org. 23 00:01:13,479 --> 00:01:18,318 Click on docs in the navigation, in of the languages and frameworks. 24 00:01:18,318 --> 00:01:21,212 It may say documentation or API. 25 00:01:21,212 --> 00:01:23,970 Generally, these terms are interchangeable. 26 00:01:23,970 --> 00:01:28,070 API stands for application programming interface. 27 00:01:28,070 --> 00:01:32,340 The documentation explains the API, or how you interface with 28 00:01:32,340 --> 00:01:36,760 the objects in the language, environment, or framework that you're working in. 29 00:01:38,000 --> 00:01:41,040 In other words, the API is the objects and 30 00:01:41,040 --> 00:01:44,920 methods that you can use in any given environment. 31 00:01:44,920 --> 00:01:45,640 In the browser, 32 00:01:45,640 --> 00:01:50,746 the API is called the document object model to interface with web pages. 33 00:01:50,746 --> 00:01:55,740 With node.js, the API is what you interface with the file system, 34 00:01:55,740 --> 00:01:59,520 or other APIs like making HTTP requests. 35 00:01:59,520 --> 00:02:03,240 Don't worry if the website looks different from how it looks on your screen. 36 00:02:03,240 --> 00:02:05,610 Websites are always being updated. 37 00:02:05,610 --> 00:02:09,910 As a developer, we must be adaptive to our ever changing surroundings. 38 00:02:09,910 --> 00:02:15,650 Let's click on the API closest to the version of node that you've got installed, 39 00:02:15,650 --> 00:02:18,030 or is in Workspaces. 40 00:02:18,030 --> 00:02:22,720 Looking in the Table of Contents, there's a link to about the docs. 41 00:02:23,970 --> 00:02:28,680 I want to point out the section at the bottom called the Stability Index. 42 00:02:28,680 --> 00:02:31,390 Zero means you shouldn't be using this anymore, 43 00:02:31,390 --> 00:02:35,610 because it's deprecated, one is experimental, and 44 00:02:35,610 --> 00:02:39,970 is something that many come in the future, but it could be removed. 45 00:02:39,970 --> 00:02:40,860 So you've been warned. 46 00:02:42,020 --> 00:02:46,300 Whilst indexes two and three stable unlocked, 47 00:02:46,300 --> 00:02:50,230 it shouldn't change, unless something serious happens. 48 00:02:50,230 --> 00:02:54,220 So, you say if using these in your applications, as a developer, 49 00:02:54,220 --> 00:02:57,160 you'll be making difficult decisions all of the time. 50 00:02:57,160 --> 00:03:01,960 And choosing what API you should use of or another is one of those times. 51 00:03:01,960 --> 00:03:07,380 But in general, the rule of thumb is to stick to stable unlocked APIs. 52 00:03:07,380 --> 00:03:09,920 Now let's go back to the API docs, and 53 00:03:09,920 --> 00:03:14,110 take a look at some of the things that you can do with node.js. 54 00:03:14,110 --> 00:03:17,530 Listed in the table of contents are all the APIs that 55 00:03:17,530 --> 00:03:19,930 come out of the box with node.js. 56 00:03:19,930 --> 00:03:23,010 All should include their own stability index. 57 00:03:23,010 --> 00:03:26,660 Some of the most common APIs that you'll use are the console, 58 00:03:26,660 --> 00:03:29,850 which is the way to output strings to the console. 59 00:03:30,890 --> 00:03:36,750 File system, these are where the APIs to read and write files are found. 60 00:03:36,750 --> 00:03:41,540 And HTTP and HTTPS, for creating web servers, or 61 00:03:41,540 --> 00:03:44,780 going out to the Internet to retrieve information. 62 00:03:44,780 --> 00:03:47,780 Let's take a look at one example in HTTPS. 63 00:03:48,800 --> 00:03:51,260 When we click through, there's a table of contents. 64 00:03:52,580 --> 00:03:57,465 This is where you can skim through the possible objects, methods, 65 00:03:57,465 --> 00:04:02,193 and properties related to the HTTPS web requests and responses. 66 00:04:02,193 --> 00:04:06,270 Let's take a look at https.get. 67 00:04:06,270 --> 00:04:10,739 The first line of code is including the HTTPS methods to 68 00:04:10,739 --> 00:04:15,228 handle what requests to HTTPS, secure HTTP service. 69 00:04:15,228 --> 00:04:20,780 The https.get method has a string of a URL 70 00:04:20,780 --> 00:04:26,980 pointing to the secure sites encrypted.google.com. 71 00:04:26,980 --> 00:04:30,000 The second argument is an arrow function. 72 00:04:30,000 --> 00:04:31,370 I call back. 73 00:04:31,370 --> 00:04:34,570 If you're unfamiliar with this syntax, see the teacher's notes for 74 00:04:34,570 --> 00:04:35,920 some material to watch. 75 00:04:37,040 --> 00:04:42,910 In our analogy before, the request is us sending out an invoice. 76 00:04:42,910 --> 00:04:47,100 The call back is the client sending the payment to us. 77 00:04:47,100 --> 00:04:50,650 Google's server is sending a response to us. 78 00:04:50,650 --> 00:04:52,842 Res is short for response. 79 00:04:52,842 --> 00:04:57,570 Because node.js is built with the non-blocking model in mind, 80 00:04:57,570 --> 00:05:03,450 the application can still be doing other things, while we write for the response. 81 00:05:03,450 --> 00:05:08,972 Using callbacks is a common pattern you'll see in building node.js applications. 82 00:05:08,972 --> 00:05:14,140 A lot of built in node.js APIs require callback functions. 83 00:05:14,140 --> 00:05:19,165 A callback function gets executed when the program is finished a particular task, 84 00:05:19,165 --> 00:05:21,670 or an event has occurred. 85 00:05:21,670 --> 00:05:26,300 If you build Ajax applications before, you don't have to halt your job script 86 00:05:26,300 --> 00:05:30,280 program while you wait for the response from the server. 87 00:05:30,280 --> 00:05:34,290 You give it a callback to run once the data has been downloaded. 88 00:05:34,290 --> 00:05:38,970 If you've ever interacted with user events like mouse clicks or keyboard presses, 89 00:05:38,970 --> 00:05:42,680 you don't halt the program from running until the event occurs. 90 00:05:42,680 --> 00:05:47,452 You give it a callback to be executed when the event occurs. 91 00:05:47,452 --> 00:05:53,630 In node.js, you can do the same thing, you provide a callback for System Events. 92 00:05:53,630 --> 00:05:56,270 So, in this example, 93 00:05:56,270 --> 00:06:00,790 the response has a system event called data associated with it. 94 00:06:02,190 --> 00:06:06,140 Then this callback prints out a piece of data. 95 00:06:06,140 --> 00:06:10,916 System events are triggered by the computer, not a user interaction. 96 00:06:10,916 --> 00:06:18,660 Process.stdout.write is just using console.log to print out information. 97 00:06:18,660 --> 00:06:23,690 This bit of code here responds to an error event on the request object. 98 00:06:23,690 --> 00:06:29,386 When you see on method being called on an object, it's responding to an event. 99 00:06:29,386 --> 00:06:32,005 You'll see this a lot in node.js. 100 00:06:32,005 --> 00:06:34,723 These lines tell you the response is status code, 101 00:06:34,723 --> 00:06:38,240 whether the request was successful or not. 102 00:06:38,240 --> 00:06:42,900 And then any special metadata called headers was sent from the server. 103 00:06:44,070 --> 00:06:46,450 If you're new to HTTP requests, 104 00:06:46,450 --> 00:06:50,790 I've included some information in the teachers notes about HTTP. 105 00:06:50,790 --> 00:06:55,800 However, we'll cover what we need in this course as we go forward. 106 00:06:55,800 --> 00:06:56,720 As a developer, 107 00:06:56,720 --> 00:07:01,090 being able to ask specific questions, short examples of your code, and 108 00:07:01,090 --> 00:07:06,440 synthesize the information from a number of sources is an absolute required skill. 109 00:07:06,440 --> 00:07:09,850 It may feel daunting at first, but it gets better, I promise. 110 00:07:09,850 --> 00:07:11,270 It takes practice, but for 111 00:07:11,270 --> 00:07:14,940 now, I'll be here to help you build these command line applications together.