1 00:00:00,008 --> 00:00:06,237 First of, let's create a function called userRoute. 2 00:00:06,237 --> 00:00:16,237 [BLANK_AUDIO] 3 00:00:20,839 --> 00:00:25,110 So the user would be slash and then the username. 4 00:00:25,110 --> 00:00:30,850 So we want to remove the first slash and that would be our username. 5 00:00:30,850 --> 00:00:35,371 So we can do var username is equal 6 00:00:35,371 --> 00:00:40,380 to the request.url, and 7 00:00:40,380 --> 00:00:45,520 we can replace 8 00:00:45,520 --> 00:00:52,580 slash with the empty string. 9 00:00:54,200 --> 00:00:56,190 So if the username 10 00:00:56,190 --> 00:01:00,219 [BLANK_AUDIO] 11 00:01:00,219 --> 00:01:05,336 length is greater than 0, so technically you could 12 00:01:05,336 --> 00:01:10,571 replace slash with just empty string on that route and 13 00:01:10,571 --> 00:01:13,960 it would just be the empty string. 14 00:01:13,960 --> 00:01:17,980 So if I went to home, it would match. 15 00:01:17,980 --> 00:01:20,340 The username would be the empty string. 16 00:01:20,340 --> 00:01:24,735 So we need to make sure that the username actually exists i.e., 17 00:01:24,735 --> 00:01:29,620 is longer than 0, and then we can do something with that username. 18 00:01:29,620 --> 00:01:34,990 So let's just look at the code here, we can do the same as this. 19 00:01:38,710 --> 00:01:44,041 But instead of search, we can just put in the username so, username. 20 00:01:45,550 --> 00:01:48,510 So we'll see the Header, we'll see the username and we'll see the Footer. 21 00:01:50,880 --> 00:01:54,360 And we'll worry about getting the actual data a little bit later on. 22 00:01:55,370 --> 00:02:00,920 But before this gets out-of-hand, we've got two routing 23 00:02:00,920 --> 00:02:06,770 functions in our app JS file, and it could get a little bit messy. 24 00:02:06,770 --> 00:02:11,674 So what we should do is compartmentalize our code, and 25 00:02:11,674 --> 00:02:18,079 we should create a router JS file which holds all the routes for our app. 26 00:02:18,079 --> 00:02:24,740 So let's do New File and we'll call it router.js. 27 00:02:24,740 --> 00:02:31,811 [BLANK_AUDIO] 28 00:02:31,811 --> 00:02:37,263 And let's just cut these two handlers out 29 00:02:37,263 --> 00:02:42,260 of our app.js and paste them in here. 30 00:02:43,700 --> 00:02:48,450 And let's just get rid of these numbers now cuz they don't really make any sense 31 00:02:48,450 --> 00:02:53,400 in the context of the app being split up into separate files now. 32 00:02:54,870 --> 00:02:56,261 So, we create the web server. 33 00:02:56,261 --> 00:03:01,416 And we can require 34 00:03:01,416 --> 00:03:08,290 the router.js file and 35 00:03:08,290 --> 00:03:13,101 let's do that, 36 00:03:13,101 --> 00:03:19,286 var router is equal to 37 00:03:19,286 --> 00:03:25,826 require router.js. 38 00:03:25,826 --> 00:03:33,839 And we need to export the routes in our router.js file. 39 00:03:33,839 --> 00:03:35,760 So, at the bottom here, 40 00:03:35,760 --> 00:03:44,970 we can do module.exports_home 41 00:03:44,970 --> 00:03:48,540 is equal to homeRoute and 42 00:03:48,540 --> 00:03:56,390 we can do module.exports_user is equal to userRoute. 43 00:03:56,390 --> 00:04:01,210 But you know what, the route at the end of the function name is kind of redundant. 44 00:04:01,210 --> 00:04:06,724 So let's just get rid of that, and get rid of that, 45 00:04:06,724 --> 00:04:11,992 get rid of that there, and get rid of that there. 46 00:04:11,992 --> 00:04:16,367 So now in our app.js, we're requiring the router and 47 00:04:16,367 --> 00:04:20,690 that means we can access those methods on the router. 48 00:04:20,690 --> 00:04:26,717 So we can do router.home and 49 00:04:26,717 --> 00:04:30,240 router.user. 50 00:04:31,910 --> 00:04:35,730 So, if we ever need to create new routes we can just go in the router file, 51 00:04:35,730 --> 00:04:39,010 export them and then just include them here like this. 52 00:04:39,010 --> 00:04:42,731 So, let's try this out in our browser. 53 00:04:42,731 --> 00:04:51,312 [BLANK_AUDIO] 54 00:04:51,312 --> 00:04:52,485 Oh, there's an error. 55 00:04:52,485 --> 00:04:56,155 Can't find the file router.js. 56 00:04:56,155 --> 00:05:02,727 [BLANK_AUDIO] 57 00:05:02,727 --> 00:05:08,060 Oh, because I didn't do the path to the router.js file. 58 00:05:08,060 --> 00:05:10,000 So, that's why. 59 00:05:10,000 --> 00:05:11,259 And that should fix it, cool. 60 00:05:11,259 --> 00:05:18,019 [BLANK_AUDIO] 61 00:05:18,019 --> 00:05:23,030 So, the home route is working still, so there's Header, Search, and Footer. 62 00:05:24,990 --> 00:05:26,450 And then let's do chalkers now. 63 00:05:27,910 --> 00:05:30,330 And it shows Header, Chalkers, and Footer. 64 00:05:30,330 --> 00:05:32,243 So, let's try a different username. 65 00:05:32,243 --> 00:05:36,670 [BLANK_AUDIO] 66 00:05:36,670 --> 00:05:41,014 joykestan2, and 67 00:05:41,014 --> 00:05:48,350 even we can do chalkers123. 68 00:05:48,350 --> 00:05:50,601 [BLANK_AUDIO] 69 00:05:50,601 --> 00:05:54,486 And it still works because we're not going out on the internet and 70 00:05:54,486 --> 00:05:58,724 we're not going to the Treehouse API and pulling information from it so 71 00:05:58,724 --> 00:06:01,280 it's not erroring at the moment, cool.