1 00:00:00,180 --> 00:00:00,560 Okay. 2 00:00:00,570 --> 00:00:01,380 Welcome back. 3 00:00:01,410 --> 00:00:06,570 So in the last section, we saw the syntax to add a single route to an express app. 4 00:00:06,750 --> 00:00:10,780 Our app has one URL that you can request to get a response. 5 00:00:10,800 --> 00:00:16,590 Everything else, we get that error message that says cannot get and then whatever you tried to get, 6 00:00:16,590 --> 00:00:17,900 whatever you tried to request. 7 00:00:17,910 --> 00:00:19,560 So we'll try fixing that now. 8 00:00:20,340 --> 00:00:22,260 We're going to add some more routes. 9 00:00:22,680 --> 00:00:25,260 So the first one we'll do is going to be silly. 10 00:00:25,260 --> 00:00:25,980 It's a joke. 11 00:00:25,980 --> 00:00:32,400 Root When you go to slash joke, it just gives you a joke rather than just doing hello or hi or goodbye 12 00:00:32,430 --> 00:00:33,430 or doing a joke. 13 00:00:33,450 --> 00:00:40,530 So when we request from a computer or from a phone even to our URL, and then we have our express app 14 00:00:40,530 --> 00:00:42,780 that we're requesting this time. 15 00:00:43,490 --> 00:00:47,390 Our request is, hey, I would like the slash joke page. 16 00:00:47,660 --> 00:00:49,160 So that's like this. 17 00:00:49,160 --> 00:00:56,930 Rather than just hitting enter here, we would go to slash joke and yeah, you're usually not typing 18 00:00:56,930 --> 00:00:59,360 these URLs manually, you're clicking on links. 19 00:00:59,360 --> 00:01:03,980 So maybe there would be a link that takes us to slash joke just as there are links down here. 20 00:01:03,980 --> 00:01:06,540 This is an error page for Cloud9, but there are links. 21 00:01:06,560 --> 00:01:08,720 Each one takes us to this. 22 00:01:08,720 --> 00:01:11,750 One is c 9.0 slash support. 23 00:01:11,750 --> 00:01:15,050 If you can see that in the bottom left, very tiny here. 24 00:01:15,680 --> 00:01:17,150 You can see slash support. 25 00:01:17,150 --> 00:01:22,700 So there's code that is listening, that's taking this slash support and it's figuring out, oh, we 26 00:01:22,700 --> 00:01:28,850 need to show the support page, the need help, all these buttons on support anyways. 27 00:01:28,850 --> 00:01:31,190 So we're going to ask for slash joke. 28 00:01:31,280 --> 00:01:36,710 Our Express app is going to say, sure thing, here you go and send back a really stupid joke. 29 00:01:37,160 --> 00:01:39,440 What do you call a dog that does magic tricks? 30 00:01:40,070 --> 00:01:42,050 A Libra cadaver door? 31 00:01:42,650 --> 00:01:46,220 I had to pick something that is politically neutral. 32 00:01:46,370 --> 00:01:47,870 It's not going to offend anybody. 33 00:01:48,080 --> 00:01:50,690 So we're just going with the abracadabra door. 34 00:01:51,050 --> 00:01:53,600 So to implement that, it's very similar to what we've done. 35 00:01:53,600 --> 00:02:00,410 We just basically duplicate the code we have, except we change the root, the string of what we're 36 00:02:00,410 --> 00:02:01,460 trying to match. 37 00:02:01,460 --> 00:02:07,040 So basically in our app JS file will have two questions which are pseudo code. 38 00:02:07,070 --> 00:02:09,889 The first one is did the client request slash? 39 00:02:09,889 --> 00:02:11,060 That's what we already have. 40 00:02:11,480 --> 00:02:17,240 If they did respond with Welcome to the home page, then we'll also have did the client request slash 41 00:02:17,240 --> 00:02:20,240 joke and if they did respond with knock knock. 42 00:02:20,240 --> 00:02:26,060 So only one of these is going to run every request because you can't send a request that is simultaneously 43 00:02:26,060 --> 00:02:27,980 to slash and slash joke. 44 00:02:27,980 --> 00:02:31,130 So we get one response depending on what we request. 45 00:02:31,850 --> 00:02:34,670 So without further ado, this is all we have to do. 46 00:02:35,390 --> 00:02:36,080 I apologize. 47 00:02:36,080 --> 00:02:40,790 It's kind of long because the joke is long, otherwise the code is pretty short. 48 00:02:41,720 --> 00:02:45,560 Get slash joke, function request response. 49 00:02:45,830 --> 00:02:53,000 And then in here we just have a joke variable equal to our dumb joke and then we're doing a resend joke. 50 00:02:53,660 --> 00:02:56,030 So if we compare that over here. 51 00:02:57,440 --> 00:02:59,900 To our existing route is very, very similar. 52 00:03:00,050 --> 00:03:04,470 So we'll get used to typing forget. 53 00:03:04,490 --> 00:03:05,540 Yeah, that was just terrible. 54 00:03:05,540 --> 00:03:06,670 But you do it a lot. 55 00:03:06,990 --> 00:03:09,200 Target and then we'll do joke. 56 00:03:10,130 --> 00:03:13,820 And that's just something I picked arbitrarily, right, to have a joke about. 57 00:03:13,850 --> 00:03:18,770 When we're actually working with our app, we'll talk about the route we need for join us. 58 00:03:18,770 --> 00:03:23,570 But for now we're working with joke, request and response. 59 00:03:24,950 --> 00:03:29,000 Once again, we can call these whatever we want, but rec and red is pretty standard. 60 00:03:29,000 --> 00:03:31,520 Then in here I'm just going to copy the joke. 61 00:03:31,550 --> 00:03:34,070 I don't want to make you watch me type that. 62 00:03:35,030 --> 00:03:35,420 All right. 63 00:03:35,420 --> 00:03:36,440 So we have that. 64 00:03:36,740 --> 00:03:39,530 And what we can do is the same thing you did earlier. 65 00:03:39,530 --> 00:03:44,290 Console.log requested the joke root. 66 00:03:45,260 --> 00:03:46,130 Quick quiz. 67 00:03:46,130 --> 00:03:51,920 What will happen when I run Node app JS When I just run it, nothing will happen unless we had an error. 68 00:03:51,920 --> 00:03:55,130 But what will happen when I make a request to slash joke? 69 00:03:55,130 --> 00:04:02,090 And the easiest way to do that is if I do preview preview running application and if I change this to 70 00:04:02,090 --> 00:04:06,140 slash joke here or I could go over here and do it in a separate window. 71 00:04:06,140 --> 00:04:11,090 But if I do slash joke and I hit enter, what do you think will happen? 72 00:04:14,440 --> 00:04:15,810 It just keeps spinning and spinning. 73 00:04:15,820 --> 00:04:19,329 We get requests to the joke route, which is what we wanted. 74 00:04:19,480 --> 00:04:20,920 We cancelled out logged. 75 00:04:20,950 --> 00:04:25,720 That means this route is being hit and we're running this code. 76 00:04:25,900 --> 00:04:29,080 But now over here, it's still spinning and spinning. 77 00:04:29,140 --> 00:04:29,650 Spinning. 78 00:04:29,650 --> 00:04:31,210 And eventually it will stop. 79 00:04:31,210 --> 00:04:31,810 And it will. 80 00:04:31,990 --> 00:04:35,870 It will give us an error message saying cannot get joke. 81 00:04:35,890 --> 00:04:38,510 Basically that's saying, you know, I tried. 82 00:04:38,530 --> 00:04:39,640 We waited and waited and waited. 83 00:04:39,640 --> 00:04:41,590 But there was no response from the server. 84 00:04:41,620 --> 00:04:46,870 So the server received it and we did something, but we never sent anything back because we're missing 85 00:04:46,870 --> 00:04:47,920 resend. 86 00:04:47,920 --> 00:04:49,540 So very simple remedy. 87 00:04:50,380 --> 00:04:50,800 Get rid. 88 00:04:50,830 --> 00:04:55,180 We can have this console.log if we want it, but it served its purpose. 89 00:04:55,210 --> 00:04:59,080 Now let's do a red send and all we want to send is the joke. 90 00:05:01,030 --> 00:05:02,980 So I'm going to stop this one. 91 00:05:02,980 --> 00:05:07,120 Well, actually, I'll just restart the server and that will kill it immediately. 92 00:05:07,960 --> 00:05:10,180 Now I'll do node JS again. 93 00:05:10,690 --> 00:05:11,710 Make this bigger. 94 00:05:13,200 --> 00:05:16,200 If I can get it, go to slash joke and hit enter. 95 00:05:16,200 --> 00:05:19,400 And now I get this hilarious joke we've never heard before. 96 00:05:19,410 --> 00:05:21,630 What do you call a dog that does magic tricks? 97 00:05:22,530 --> 00:05:23,910 Elaborate cadaver door. 98 00:05:24,180 --> 00:05:24,900 It's pretty good. 99 00:05:24,900 --> 00:05:25,710 Pretty good. 100 00:05:25,740 --> 00:05:28,760 Whoever came up with that one, and that's it. 101 00:05:28,770 --> 00:05:32,040 We have our two routes now, so we could toggle between them. 102 00:05:32,400 --> 00:05:33,320 What we eBay. 103 00:05:33,330 --> 00:05:34,650 I don't know where that came from. 104 00:05:35,070 --> 00:05:35,840 Here we go. 105 00:05:35,850 --> 00:05:41,130 So I've got the home root slash, the root root or slash joke. 106 00:05:42,550 --> 00:05:43,320 And that's it. 107 00:05:43,330 --> 00:05:49,270 So really the point here is just to show you, you can have two routes and usually applications have 108 00:05:49,270 --> 00:05:55,300 a ton of roots 1020 depending on how complex the application is, something like Cloud nine or Udemy 109 00:05:55,300 --> 00:05:58,030 might have 50 or 100 different things. 110 00:05:58,040 --> 00:06:03,060 Well, hundreds extreme, but there's a lot of different routes to wrap up. 111 00:06:03,070 --> 00:06:04,660 Let's add one last route. 112 00:06:04,660 --> 00:06:06,160 This one is a little different. 113 00:06:06,160 --> 00:06:10,180 What it will do is return a random number randomly generated. 114 00:06:10,180 --> 00:06:14,890 So it's not going to be hardcoded in like you've reached the home page or what do you call it, a dog 115 00:06:14,890 --> 00:06:15,670 that does magic tricks. 116 00:06:15,800 --> 00:06:16,270 Abracadabra. 117 00:06:16,450 --> 00:06:16,750 Ha ha. 118 00:06:16,750 --> 00:06:17,620 Very funny joke. 119 00:06:17,740 --> 00:06:23,440 Rather than that, it's going to be dynamically generated, but everything else is the same, so it's 120 00:06:23,440 --> 00:06:23,830 apt. 121 00:06:23,860 --> 00:06:32,440 Get slash and we're doing random num or number either one i did num in the notes comma function request 122 00:06:32,440 --> 00:06:33,580 in response again. 123 00:06:33,580 --> 00:06:39,250 So whatever's in here will only be executed whenever someone requests slash random num. 124 00:06:39,490 --> 00:06:47,140 So we'll do our app send or excuse me resend and let's just start by just hard coding a number like 125 00:06:47,140 --> 00:06:47,770 seven. 126 00:06:48,910 --> 00:06:55,420 I'll do it as a string just to be safe if we restart the server now go over here. 127 00:06:55,690 --> 00:06:59,380 Joke still works, home still works. 128 00:06:59,770 --> 00:07:04,960 But now we have random underscore num and I get the number seven. 129 00:07:05,410 --> 00:07:10,840 So now all that's left is to generate a random number and the way we do it in JavaScript is kind of 130 00:07:10,840 --> 00:07:11,590 obnoxious. 131 00:07:11,590 --> 00:07:13,300 It's much easier in other languages. 132 00:07:13,300 --> 00:07:22,570 So there is a math dot random and what this will do, I'll actually just show you right now, if I copy 133 00:07:22,570 --> 00:07:27,880 it over, I'm going to stop the server and I'm going to open just regular node, which is not something 134 00:07:27,880 --> 00:07:33,520 I've shown you, but Node has just like my SQL has a CLI where we can type things and get a response 135 00:07:33,520 --> 00:07:34,120 immediately. 136 00:07:34,120 --> 00:07:38,830 You can do one plus one and get to Node has the same thing. 137 00:07:38,830 --> 00:07:43,510 If you just type Node without a file name, then it opens up a node shell. 138 00:07:43,660 --> 00:07:50,680 So if I do math random and I don't capitalize that ah, now I do math at random. 139 00:07:51,340 --> 00:07:55,600 Notice that it gives me a random number between the zero and one. 140 00:07:55,600 --> 00:07:58,510 It's really annoying that it does it this way, in my opinion. 141 00:07:58,510 --> 00:08:05,320 So if we want, let's say something between one and ten, there's a couple of steps involved. 142 00:08:05,350 --> 00:08:07,540 We need to generate our math random. 143 00:08:07,810 --> 00:08:09,640 We need to multiply it by ten. 144 00:08:09,640 --> 00:08:10,930 So we'll start with that. 145 00:08:11,470 --> 00:08:11,660 Okay. 146 00:08:11,830 --> 00:08:15,760 So now we're getting 1.7, 7.4, 8.6. 147 00:08:16,270 --> 00:08:19,990 Then what we need to do is get rid of that decimal so you can either round. 148 00:08:19,990 --> 00:08:22,900 What's more standard is to do a math flaw. 149 00:08:24,070 --> 00:08:27,820 So what that will do is just chop off the decimals. 150 00:08:27,820 --> 00:08:31,300 So we end up with eight, nine, eight two. 151 00:08:31,300 --> 00:08:34,510 But then we have a problem, which is we never actually hit ten. 152 00:08:34,539 --> 00:08:39,400 We're doing a number between zero and nine, which those have to trust me. 153 00:08:39,400 --> 00:08:40,000 That's how it works. 154 00:08:40,000 --> 00:08:45,730 But you can see we have zero here and it's because when we did our original math random this is a number 155 00:08:45,730 --> 00:08:49,300 between zero and one non including or not including one. 156 00:08:49,450 --> 00:08:51,970 So up to a .99999. 157 00:08:52,330 --> 00:08:55,060 So all we want to do is then shift it by one. 158 00:08:55,060 --> 00:08:59,410 So it's kind of a lot of work to generate a random number between one and ten. 159 00:08:59,410 --> 00:09:04,450 But now we've got it just to prove, okay, there's a ten, we're in the clear. 160 00:09:04,510 --> 00:09:06,430 So now we'll just make that a variable. 161 00:09:06,430 --> 00:09:08,830 We'll just call it X, maybe equals. 162 00:09:08,920 --> 00:09:09,460 Well, maybe. 163 00:09:09,460 --> 00:09:10,660 No, it's a better name. 164 00:09:10,660 --> 00:09:14,140 Make it a little clearer and then we'll just send it back. 165 00:09:14,140 --> 00:09:24,010 We'll say your and I think I said lucky number is plus num and that will just concatenate it in there. 166 00:09:25,060 --> 00:09:25,750 Let's try it. 167 00:09:25,750 --> 00:09:32,950 So to get out of Node, we just do control C twice now we actually execute our app is. 168 00:09:34,660 --> 00:09:37,520 Now if I go to random num, it says my lucky number is three. 169 00:09:37,540 --> 00:09:38,200 Now it's ten. 170 00:09:38,200 --> 00:09:38,950 Now it's eight. 171 00:09:39,340 --> 00:09:40,630 Eight again four. 172 00:09:40,660 --> 00:09:41,590 You get the picture. 173 00:09:42,070 --> 00:09:46,990 And of course, we still have our home root and our refreshingly adorable and hilarious. 174 00:09:47,140 --> 00:09:47,890 Abracadabra, Dora. 175 00:09:47,920 --> 00:09:48,450 Joke. 176 00:09:48,910 --> 00:09:50,440 Okay, we're done here. 177 00:09:50,740 --> 00:09:55,810 Moving on in our next video to actually integrating my SQL with this Web app. 178 00:09:55,840 --> 00:09:56,650 Big step.