1 00:00:00,000 --> 00:00:04,489 [MUSIC] 2 00:00:04,489 --> 00:00:05,860 We have the server up and running. 3 00:00:05,860 --> 00:00:10,750 But it responds the same way, no matter what path we add to the end of the URL. 4 00:00:10,750 --> 00:00:16,110 Let's make sure that that doesn't happen by dealing with the home and user routes. 5 00:00:16,110 --> 00:00:22,090 So, let's first create a function called homeRoute. 6 00:00:22,090 --> 00:00:27,508 [BLANK_AUDIO] 7 00:00:27,508 --> 00:00:34,004 And we'll wrap all the sudo code that we wrote for handling the, the home. 8 00:00:34,004 --> 00:00:38,905 We'll want the request and 9 00:00:38,905 --> 00:00:44,130 response as parameters. 10 00:00:44,130 --> 00:00:49,626 So similar to the createServer up here where we've got the request and response, 11 00:00:49,626 --> 00:00:57,110 we'll be putting the handlers in here that will deal with all the different routes. 12 00:00:58,240 --> 00:01:02,690 So let's move that from there down to show 13 00:01:02,690 --> 00:01:08,070 search and I'll do response. 14 00:01:08,070 --> 00:01:13,489 Right, we'll just get rid of this interval, we don't need that anymore. 15 00:01:13,489 --> 00:01:18,450 And every web page that we will have will have a header or 16 00:01:18,450 --> 00:01:23,070 a head where all the things in the hat, head tags are similar. 17 00:01:23,070 --> 00:01:26,597 So we'll just type header here and then we'll have. 18 00:01:26,597 --> 00:01:32,314 [BLANK_AUDIO] 19 00:01:32,314 --> 00:01:38,692 Search here and at the end we'll have the footer all at the end of the body. 20 00:01:38,692 --> 00:01:47,802 [BLANK_AUDIO] 21 00:01:47,802 --> 00:01:55,550 So we can handle the hermRoute request and response. 22 00:01:55,550 --> 00:01:59,970 And let's just try this out to make sure that what we've done hasn't broken 23 00:01:59,970 --> 00:02:00,670 anything. 24 00:02:00,670 --> 00:02:04,400 So we create the server. 25 00:02:04,400 --> 00:02:09,040 The homeRoute handler gets the request and the response, 26 00:02:09,040 --> 00:02:13,500 the same request and response that the server gets in. 27 00:02:13,500 --> 00:02:18,900 And then we can choose to write to that response with a header, 28 00:02:18,900 --> 00:02:21,120 a search field, and a footer. 29 00:02:21,120 --> 00:02:25,244 These are just gonna be placeholders until we get our templating working. 30 00:02:25,244 --> 00:02:36,081 [BLANK_AUDIO] 31 00:02:36,081 --> 00:02:39,360 And yeah, we've got header, search, and footer. 32 00:02:39,360 --> 00:02:44,508 So, let's see what happens when we do \chalkers. 33 00:02:44,508 --> 00:02:48,241 [BLANK_AUDIO] 34 00:02:48,241 --> 00:02:50,190 It does the same thing again. 35 00:02:50,190 --> 00:02:54,765 So, we need to restrict that for, just the URL for the homeRoute. 36 00:02:55,830 --> 00:02:56,780 So how did we do that? 37 00:02:58,020 --> 00:03:00,010 So let's go to the node.js website. 38 00:03:01,530 --> 00:03:07,910 Click on Docs, then API Docs, and then HTTP. 39 00:03:07,910 --> 00:03:10,480 And let's take a look at this request here. 40 00:03:10,480 --> 00:03:15,510 It says the, the request is an instance of an IncomingMessage. 41 00:03:15,510 --> 00:03:22,143 So an IncomingMessage is an object that is created by the HTTP server or 42 00:03:22,143 --> 00:03:28,720 the ClientRequest and is passed in as the first argument of the request response. 43 00:03:28,720 --> 00:03:32,540 So the, the request is the incoming message. 44 00:03:32,540 --> 00:03:35,380 Now what kind of properties do we have on an incoming message? 45 00:03:35,380 --> 00:03:40,300 We've got http version, headers, raw headers, trailers, 46 00:03:40,300 --> 00:03:44,340 raw trailers method URL. 47 00:03:44,340 --> 00:03:46,710 So let's take a look at this. 48 00:03:46,710 --> 00:03:50,365 So. [BLANK_AUDIO] 49 00:03:50,365 --> 00:03:53,710 The URL will be status/ryan. 50 00:03:53,710 --> 00:03:59,900 So if, if you were typing in /status? 51 00:03:59,900 --> 00:04:03,890 Name is equal to Ryan, the URL would be the whole thing like that. 52 00:04:03,890 --> 00:04:10,028 So we can do the request.url and just look for the slash. 53 00:04:10,028 --> 00:04:12,856 So, if 54 00:04:12,856 --> 00:04:21,300 request.url. 55 00:04:21,300 --> 00:04:25,870 So that's the property on the incoming message, which is the request, 56 00:04:25,870 --> 00:04:34,680 is equal to forward slash. 57 00:04:34,680 --> 00:04:35,580 Then just do this. 58 00:04:37,080 --> 00:04:37,580 Cool. 59 00:04:39,390 --> 00:04:41,470 Let's try this out, so. 60 00:04:41,470 --> 00:04:44,058 Press Ctrl+C. 61 00:04:44,058 --> 00:04:46,130 That kills the app. 62 00:04:46,130 --> 00:04:46,930 Let's run it again. 63 00:04:48,040 --> 00:04:49,690 Go back to port 3000. 64 00:04:49,690 --> 00:04:51,560 Okay that's cool. 65 00:04:51,560 --> 00:04:54,840 So the home route is actually showing now. 66 00:04:54,840 --> 00:04:58,054 So let's go to \chalkers. 67 00:04:59,590 --> 00:05:03,030 Hit Enter and, if, you may think, oh, it's still showing it. 68 00:05:03,030 --> 00:05:08,000 But actually, down here in the status bar at the bottom, 69 00:05:08,000 --> 00:05:11,620 it says it's waiting, because we're not even handling it anymore. 70 00:05:11,620 --> 00:05:13,570 We're not giving a response. 71 00:05:13,570 --> 00:05:16,880 This will probably just time out on the browser, and 72 00:05:16,880 --> 00:05:19,720 it will say that it doesn't exist, this path. 73 00:05:19,720 --> 00:05:21,092 So, there's a timeout error. 74 00:05:21,092 --> 00:05:24,399 [BLANK_AUDIO] 75 00:05:24,399 --> 00:05:27,250 And eventually you'll see this, Bad Gateway. 76 00:05:27,250 --> 00:05:32,180 Basically, the server hasn't responded at all, or there's something wrong. 77 00:05:32,180 --> 00:05:36,460 So the, the server has not done its job properly. 78 00:05:36,460 --> 00:05:40,580 So, in the next video, we're gonna take a look at creating that user route.