1 00:00:00,600 --> 00:00:02,220 Instructor: We've now got Mongo set up, 2 00:00:02,220 --> 00:00:04,860 we've got MongoDB running in the background 3 00:00:04,860 --> 00:00:07,470 and we've got Robomongo to inspect our database 4 00:00:07,470 --> 00:00:09,330 after we make changes to it. 5 00:00:09,330 --> 00:00:10,890 I think we're at a great place right now 6 00:00:10,890 --> 00:00:13,320 where we could start actually saving user models 7 00:00:13,320 --> 00:00:15,900 to our database based on incoming requests. 8 00:00:15,900 --> 00:00:18,873 So in other words, implement our signup route. 9 00:00:20,459 --> 00:00:21,660 To implement our signup route 10 00:00:21,660 --> 00:00:23,370 I'm gonna want to change the structure 11 00:00:23,370 --> 00:00:25,800 of our router a little bit here. 12 00:00:25,800 --> 00:00:26,880 Our current route handler 13 00:00:26,880 --> 00:00:30,150 has all of its logic inside the router. 14 00:00:30,150 --> 00:00:31,440 This is good for now 15 00:00:31,440 --> 00:00:33,600 when we've got just this one route handler 16 00:00:33,600 --> 00:00:35,280 but you could kinda imagine that if we start 17 00:00:35,280 --> 00:00:37,290 to have a lot of different route handlers 18 00:00:37,290 --> 00:00:38,790 with a lot of logic 19 00:00:38,790 --> 00:00:41,880 this router file is gonna grow to be very large. 20 00:00:41,880 --> 00:00:45,510 So let's implement our authentication logic 21 00:00:45,510 --> 00:00:47,700 or our signup logic, but we're going to do so 22 00:00:47,700 --> 00:00:49,200 in a separate file 23 00:00:49,200 --> 00:00:51,420 and then pull it into our router. 24 00:00:51,420 --> 00:00:54,173 So let's look at a diagram of what this might look like. 25 00:00:57,056 --> 00:00:59,220 So we're going to have an incoming request 26 00:00:59,220 --> 00:01:01,590 and it's always gonna go through the router 27 00:01:01,590 --> 00:01:02,940 but the router is gonna decide 28 00:01:02,940 --> 00:01:05,583 where it wants to send the request off to. 29 00:01:08,820 --> 00:01:09,653 In this case 30 00:01:09,653 --> 00:01:12,300 I've created three kind of imaginary controllers 31 00:01:12,300 --> 00:01:16,230 one for comments, one for authentication, and one for posts. 32 00:01:16,230 --> 00:01:18,210 We're only gonna have an authentication controller. 33 00:01:18,210 --> 00:01:20,703 The other two here are just here for examples. 34 00:01:21,720 --> 00:01:24,883 We're going to define the logic to handle these requests 35 00:01:24,883 --> 00:01:27,510 inside of what we call controllers. 36 00:01:27,510 --> 00:01:28,980 So we're gonna have one controller 37 00:01:28,980 --> 00:01:32,580 kind of for each resource you can kind of think of. 38 00:01:32,580 --> 00:01:33,810 And then each controller 39 00:01:33,810 --> 00:01:36,300 is gonna be individually responsible 40 00:01:36,300 --> 00:01:39,060 for creating a response and actually sending it back 41 00:01:39,060 --> 00:01:42,750 to whoever made the request in the first place. 42 00:01:42,750 --> 00:01:43,920 So our goal right now 43 00:01:43,920 --> 00:01:46,380 is to create this authentication controller 44 00:01:46,380 --> 00:01:48,810 and create our signup route handler 45 00:01:48,810 --> 00:01:50,940 and place that inside of our router. 46 00:01:50,940 --> 00:01:52,790 So let's go ahead and give it a shot. 47 00:01:54,420 --> 00:01:55,740 In my route directory 48 00:01:55,740 --> 00:01:58,560 I'm gonna create a new folder called controllers 49 00:01:58,560 --> 00:01:59,820 and this is where is where I would put 50 00:01:59,820 --> 00:02:02,940 all my different controllers inside my application. 51 00:02:02,940 --> 00:02:05,040 I'm going to make a new file in here 52 00:02:05,040 --> 00:02:07,743 and I'm just gonna call it authentication.js. 53 00:02:08,699 --> 00:02:11,130 Now, this is where I'm gonna put my logic 54 00:02:11,130 --> 00:02:13,253 to runt or to process a request. 55 00:02:13,253 --> 00:02:16,770 So this is where we're going to pull in a request object, 56 00:02:16,770 --> 00:02:19,710 a response object, we're gonna put in some logic 57 00:02:19,710 --> 00:02:22,263 and then we're going to ultimately respond to it. 58 00:02:24,390 --> 00:02:25,590 So to do this 59 00:02:25,590 --> 00:02:29,280 we're going to define a function and then export it. 60 00:02:29,280 --> 00:02:31,050 So we'll try to export it 61 00:02:31,050 --> 00:02:33,420 and we're going to use the exports keyword. 62 00:02:33,420 --> 00:02:36,303 And we're going to export a function called signup. 63 00:02:37,980 --> 00:02:41,100 And this will be a function that takes a request, 64 00:02:41,100 --> 00:02:44,373 response, and next. 65 00:02:45,600 --> 00:02:48,210 And this is probably gonna be good for right now 66 00:02:48,210 --> 00:02:50,190 let's wire it up to our router 67 00:02:50,190 --> 00:02:51,060 and then we'll come back here 68 00:02:51,060 --> 00:02:53,710 and maybe put in a little bit more logic for testing. 69 00:02:55,830 --> 00:02:57,330 So now all we really wanna do 70 00:02:57,330 --> 00:03:00,240 is whenever a user visits a a very particular route 71 00:03:00,240 --> 00:03:03,210 we wanna send them to that authentication controller. 72 00:03:03,210 --> 00:03:06,700 So let's first import the authentication controller up top 73 00:03:07,710 --> 00:03:12,710 and we'll do so with constant authentication 74 00:03:13,170 --> 00:03:18,093 and it'll require controllers authentication. 75 00:03:19,440 --> 00:03:21,090 We don't really need this test route anymore 76 00:03:21,090 --> 00:03:22,690 so I'm gonna take it off for now 77 00:03:23,730 --> 00:03:27,000 and we're going to replace it with a route handler 78 00:03:27,000 --> 00:03:28,320 for our signup route. 79 00:03:28,320 --> 00:03:30,150 So this is going to be a post request 80 00:03:30,150 --> 00:03:31,590 because the user's gonna be posting 81 00:03:31,590 --> 00:03:33,480 some username and password 82 00:03:33,480 --> 00:03:34,710 and they're gonna post it to something 83 00:03:34,710 --> 00:03:37,200 like basically /signup. 84 00:03:37,200 --> 00:03:39,600 So let's say app.post, 85 00:03:39,600 --> 00:03:42,600 post specifically to handle a post request 86 00:03:42,600 --> 00:03:45,960 for anything posted to /signup. 87 00:03:45,960 --> 00:03:49,893 We want to run the function authentication.signup. 88 00:03:52,230 --> 00:03:53,370 So let's save this. 89 00:03:53,370 --> 00:03:55,020 I'm gonna check my Terminal real quick 90 00:03:55,020 --> 00:03:57,450 just to make sure that I didn't make any errors. 91 00:03:57,450 --> 00:03:58,920 So everything is still agreeing over here. 92 00:03:58,920 --> 00:04:00,390 I don't have any errors or anything like that. 93 00:04:00,390 --> 00:04:01,223 That's good. 94 00:04:02,640 --> 00:04:05,160 Let's go back over to our authentication controller. 95 00:04:05,160 --> 00:04:06,900 So I've got a reasonable expectation 96 00:04:06,900 --> 00:04:08,340 that this is where any request 97 00:04:08,340 --> 00:04:10,950 is gonna be piped to if I make a post request 98 00:04:10,950 --> 00:04:12,900 to the signup route. 99 00:04:12,900 --> 00:04:15,120 So let's just make sure everything's working right now. 100 00:04:15,120 --> 00:04:17,970 Let's make a response and send it back 101 00:04:17,970 --> 00:04:20,553 and we'll just say success true. 102 00:04:21,450 --> 00:04:24,300 Just some simple json that we'll send back to the client. 103 00:04:25,140 --> 00:04:27,600 So I'm gonna save this and I'm gonna flip over to Postman 104 00:04:27,600 --> 00:04:28,770 and we're gonna give it a quick shot. 105 00:04:28,770 --> 00:04:30,520 We're gonna see if this is working. 106 00:04:32,160 --> 00:04:35,580 So in Postman, I've got a post selected 107 00:04:35,580 --> 00:04:40,580 to localhost:3090/signup 108 00:04:40,590 --> 00:04:43,860 and I'm gonna send this off and I get back success, true. 109 00:04:43,860 --> 00:04:46,560 Perfect, that means that my route is working. 110 00:04:46,560 --> 00:04:47,640 This looks good. 111 00:04:47,640 --> 00:04:49,650 Let's go ahead and continue in the next section 112 00:04:49,650 --> 00:04:51,810 where we will implement the ability 113 00:04:51,810 --> 00:04:55,440 to create and save users inside of our signup handler. 114 00:04:55,440 --> 00:04:56,390 I'll see you there.