1 00:00:00,270 --> 00:00:04,470 Now that we've organized our routes into the router.js file, we can concentrate 2 00:00:04,470 --> 00:00:09,180 on getting the live student data and outputting that into our user route. 3 00:00:09,180 --> 00:00:11,580 We're here in the router.js file. 4 00:00:11,580 --> 00:00:16,820 And this is where we would want to do all the username 5 00:00:16,820 --> 00:00:21,080 profile information retrieval part of our application. 6 00:00:21,080 --> 00:00:25,090 So let's take a look at the example and let's just copy and 7 00:00:25,090 --> 00:00:26,250 paste some of this code here. 8 00:00:26,250 --> 00:00:30,990 So let's grab the required statement at the top and 9 00:00:30,990 --> 00:00:34,960 the student profile variable and go to our router. 10 00:00:35,960 --> 00:00:38,720 And at the top, just have the profile required. 11 00:00:39,840 --> 00:00:45,040 Now let's cut the variable where we set the user name 12 00:00:46,150 --> 00:00:51,372 and put that into our user route. 13 00:00:51,372 --> 00:00:56,444 So depending on whether if there is an error or not, we're gonna keep 14 00:00:56,444 --> 00:01:01,965 the response head and the header, or the top part of our document the same. 15 00:01:01,965 --> 00:01:07,040 But in the, underneath here, this is where we will 16 00:01:07,040 --> 00:01:13,531 get the JSON from treehouse, so let's just pop that there, 17 00:01:13,531 --> 00:01:18,030 and then on end, we want to show the profile. 18 00:01:19,600 --> 00:01:24,400 So remember we can just use this here. 19 00:01:24,400 --> 00:01:28,420 What happens is that the JSON body is fully received the end event is triggered. 20 00:01:28,420 --> 00:01:35,576 So a, a, a JSON object will be passed into this call back here. 21 00:01:35,576 --> 00:01:42,390 So let's just copy and paste that underneath our comment there, 22 00:01:42,390 --> 00:01:47,780 and we'll create the function, or the callback, or the handler, and 23 00:01:47,780 --> 00:01:54,960 we will call it profile JSON, and in there we will share our profile. 24 00:01:56,030 --> 00:01:59,910 And on error, we want to show a error message. 25 00:01:59,910 --> 00:02:03,600 So let's just copy and paste this down here. 26 00:02:04,900 --> 00:02:06,390 Let's move our comments up as well. 27 00:02:09,140 --> 00:02:12,502 And [BLANK_AUDIO] 28 00:02:12,502 --> 00:02:15,019 in the error handler, 29 00:02:15,019 --> 00:02:20,742 [BLANK_AUDIO] 30 00:02:20,742 --> 00:02:22,550 We get the error object. 31 00:02:25,380 --> 00:02:26,743 And we wanna show the error. 32 00:02:26,743 --> 00:02:30,989 [BLANK_AUDIO] 33 00:02:30,989 --> 00:02:34,800 Now, before we go any further. 34 00:02:34,800 --> 00:02:37,810 Let's take out this hardwired chalkers, 35 00:02:37,810 --> 00:02:42,760 because we want this to be dynamic depending on the URL you go to. 36 00:02:42,760 --> 00:02:44,740 So we have this username here. 37 00:02:44,740 --> 00:02:48,549 And if the username is greater than zero, so it's not the empty string. 38 00:02:48,549 --> 00:02:53,539 We want it to go out to the Treehouse website, get the information and 39 00:02:53,539 --> 00:02:56,646 bring it back into this profile JSON here. 40 00:02:56,646 --> 00:02:59,250 So let's just write username. 41 00:02:59,250 --> 00:03:05,632 And when that comes back, right now we'll just create a simple response, 42 00:03:05,632 --> 00:03:12,927 so we want to, if you remember when you go to teamtreehouse.com/chalkers.json, 43 00:03:12,927 --> 00:03:16,010 or whatever username .json. 44 00:03:16,010 --> 00:03:19,280 You get this massive JSON object, 45 00:03:19,280 --> 00:03:23,020 that, we won't be using all of this information in our templates. 46 00:03:23,020 --> 00:03:26,820 We can just make this a little bit more concise. 47 00:03:26,820 --> 00:03:28,840 So, we'll do that, as well. 48 00:03:28,840 --> 00:03:30,700 So, star. 49 00:03:33,290 --> 00:03:37,410 The values which we need. 50 00:03:38,870 --> 00:03:43,465 So let's call var values and 51 00:03:43,465 --> 00:03:47,303 we want the avatar URL. 52 00:03:47,303 --> 00:03:51,851 [BLANK_AUDIO] 53 00:03:51,851 --> 00:03:53,210 And we're gonna put that in there. 54 00:03:54,930 --> 00:03:55,690 What else do we need? 55 00:03:55,690 --> 00:03:58,341 We need the username. 56 00:03:58,341 --> 00:04:02,805 [BLANK_AUDIO] 57 00:04:02,805 --> 00:04:04,469 We also need the badge count. 58 00:04:04,469 --> 00:04:09,442 [BLANK_AUDIO] 59 00:04:09,442 --> 00:04:11,554 And we need the JavaScript points. 60 00:04:11,554 --> 00:04:21,554 [BLANK_AUDIO] 61 00:04:22,827 --> 00:04:24,220 And that goes there. 62 00:04:24,220 --> 00:04:25,460 Cool. 63 00:04:25,460 --> 00:04:30,520 So let's take a look at that JSON and see what the avatar URL is. 64 00:04:31,980 --> 00:04:34,060 Okay, so it's this gravitar URL here. 65 00:04:41,280 --> 00:04:43,930 And it's on the profile JSON. 66 00:04:43,930 --> 00:04:48,740 So this profile JSON, is actually the same as this root up here. 67 00:04:48,740 --> 00:04:50,600 The root of this object, so. 68 00:04:50,600 --> 00:04:54,340 You go from profile.json.gravitar URL. 69 00:04:55,940 --> 00:04:56,750 What's next? 70 00:04:56,750 --> 00:05:03,482 Username, and that's profile, oh, profile name. 71 00:05:03,482 --> 00:05:13,482 [BLANK_AUDIO] 72 00:05:17,381 --> 00:05:19,045 And then the badges. 73 00:05:19,045 --> 00:05:22,860 [BLANK_AUDIO] 74 00:05:22,860 --> 00:05:26,590 So the badges is an array because it's got these square brackets, so 75 00:05:26,590 --> 00:05:28,660 we can call the .length method on it. 76 00:05:36,220 --> 00:05:37,620 And then the JavaScript points. 77 00:05:39,260 --> 00:05:43,506 So in every Treehouse user profile there's a points 78 00:05:43,506 --> 00:05:50,690 dictionary with all the topic areas with their relevant point, so. 79 00:05:50,690 --> 00:05:52,050 We just want JavaScript. 80 00:05:53,190 --> 00:05:55,060 And remember it's case-sensitive. 81 00:05:55,060 --> 00:05:58,925 So, you need to be careful with your capitalizations. 82 00:05:58,925 --> 00:06:04,491 [BLANK_AUDIO] 83 00:06:04,491 --> 00:06:06,241 Cool. 84 00:06:06,241 --> 00:06:08,566 And, let's do a simple response. 85 00:06:08,566 --> 00:06:15,834 [BLANK_AUDIO] 86 00:06:15,834 --> 00:06:20,030 And so before when it was just doing, printed out the user name, 87 00:06:20,030 --> 00:06:22,140 we didn't have any call backs. 88 00:06:22,140 --> 00:06:27,060 We weren't going out to the internet and waiting and then doing something, so 89 00:06:27,060 --> 00:06:33,150 we need to write an end in every call back that we, we do. 90 00:06:33,150 --> 00:06:37,010 Remember with that set interval we did the end after it, and 91 00:06:37,010 --> 00:06:41,740 that callback in the set interval got executed a second after it ended. 92 00:06:41,740 --> 00:06:44,550 So now we need to wait for the data to come in. 93 00:06:44,550 --> 00:06:48,690 We do whatever we want with that data, and then we end the response. 94 00:06:48,690 --> 00:06:50,600 And the same goes for the arrow down here. 95 00:06:50,600 --> 00:06:54,548 So, I'm just gonna put the ends in both of these so 96 00:06:54,548 --> 00:06:57,655 that we don't forget to do it later on. 97 00:06:57,655 --> 00:07:03,700 [BLANK_AUDIO] 98 00:07:03,700 --> 00:07:05,290 Let's clean up some space. 99 00:07:07,530 --> 00:07:08,340 Cool. 100 00:07:08,340 --> 00:07:12,520 So let's do a simple response with this value so. 101 00:07:12,520 --> 00:07:18,116 Values.username has 102 00:07:18,116 --> 00:07:22,830 values.badges. 103 00:07:26,650 --> 00:07:31,173 So this should read chalkers has how ever many badges I have badges [LAUGH]. 104 00:07:31,173 --> 00:07:36,372 Okay, so let's try that out. 105 00:07:36,372 --> 00:07:37,530 Cool. 106 00:07:37,530 --> 00:07:40,450 So let's go to /chalkers and see if that works. 107 00:07:47,610 --> 00:07:51,720 And it says chalkers has 162 badges. 108 00:07:51,720 --> 00:07:52,890 Okay, let's try someone else. 109 00:07:54,000 --> 00:07:57,540 Let's try joykesten2. 110 00:08:00,350 --> 00:08:04,190 And as you can see she's got 130 badges, cool. 111 00:08:04,190 --> 00:08:10,000 Now let's handle the case where we type in a random user that doesn't exist, 112 00:08:10,000 --> 00:08:11,060 and see what happens. 113 00:08:13,980 --> 00:08:20,960 So in our on error, let's provide a response, and write out the error message. 114 00:08:22,070 --> 00:08:26,000 Remember, error objects have a message property. 115 00:08:27,210 --> 00:08:28,417 So, let's write that. 116 00:08:28,417 --> 00:08:31,561 [BLANK_AUDIO] 117 00:08:31,561 --> 00:08:32,820 And do a return. 118 00:08:32,820 --> 00:08:41,900 [BLANK_AUDIO] 119 00:08:41,900 --> 00:08:43,780 Let's kill our server. 120 00:08:46,030 --> 00:08:47,100 And start it up again. 121 00:08:49,090 --> 00:08:49,978 Go back to our preview. 122 00:08:49,978 --> 00:08:54,851 [BLANK_AUDIO] 123 00:08:54,851 --> 00:08:56,500 Let's go to chalkers. 124 00:08:58,870 --> 00:09:00,500 That works. 125 00:09:00,500 --> 00:09:03,821 And let's try chalkers 1, 2, 3 and hit Enter. 126 00:09:03,821 --> 00:09:05,734 [BLANK_AUDIO] 127 00:09:05,734 --> 00:09:11,220 And it says there was an error getting the profile for chalkers123, not found, cool. 128 00:09:11,220 --> 00:09:14,710 So we're showing both the information from the user and 129 00:09:14,710 --> 00:09:16,914 we're showing the error message when that occurs. 130 00:09:17,945 --> 00:09:20,965 Slowly but surely we're getting closer to our goal. 131 00:09:20,965 --> 00:09:25,775 We've got an HTTP service responding to two different routes, the home and 132 00:09:25,775 --> 00:09:28,785 user route and we're getting user information. 133 00:09:28,785 --> 00:09:29,285 Well done.