1 00:00:00,210 --> 00:00:05,340 In this video you'll be setting up the very first End Point for our REST API. 2 00:00:05,340 --> 00:00:12,210 Now the goal of this video and the next one is to focus on our endpoints that involve resource creation 3 00:00:12,450 --> 00:00:16,210 so creating new users and creating new tasks. 4 00:00:16,380 --> 00:00:21,510 Together it will work on creating new users then any next video as a challenge. 5 00:00:21,510 --> 00:00:25,020 You'll be setting up the ability to create tasks on your own. 6 00:00:25,020 --> 00:00:28,450 Let's get started by installing a few things we've used before. 7 00:00:28,470 --> 00:00:34,530 We're going to install no demand as a dev dependency and we're going to install express as a regular 8 00:00:34,530 --> 00:00:35,510 dependency. 9 00:00:35,700 --> 00:00:43,470 So down below from the terminal NPM I will first install no demand at the latest version one point eighteen 10 00:00:43,530 --> 00:00:50,820 point nine followed by the Save Dev flag to save node man as a development dependency. 11 00:00:50,820 --> 00:00:52,470 Then we're gonna go ahead and run that. 12 00:00:52,500 --> 00:00:58,290 And before we actually write any code we'll also take a quick moment to install Express into the task 13 00:00:58,350 --> 00:01:04,800 manager app once that's in place we'll focus on creating the Express server and setting up the very 14 00:01:04,830 --> 00:01:07,200 first endpoint down below. 15 00:01:07,200 --> 00:01:09,240 We're almost done installing node man. 16 00:01:09,360 --> 00:01:14,150 Now one other thing we're gonna do in this video is we're gonna refactor the Mongoose file. 17 00:01:14,190 --> 00:01:17,610 We're not going to leave our model definitions inside of here. 18 00:01:17,640 --> 00:01:20,700 We're going to create separate files for those as well. 19 00:01:20,760 --> 00:01:25,230 So we're staying nice and organized as we build out the task manager app. 20 00:01:25,260 --> 00:01:34,290 We'll talk about directory structures that scale well down below NPM I express at the latest version 21 00:01:34,320 --> 00:01:37,790 that is four point sixteen point four. 22 00:01:37,800 --> 00:01:42,060 Now once that's installed will actually be able to use both of those tools. 23 00:01:42,060 --> 00:01:46,200 Let's go ahead and set up the file that's gonna start up our application. 24 00:01:46,200 --> 00:01:52,750 So inside of the source directory we're gonna go ahead and have a new file index dot J S. 25 00:01:52,920 --> 00:01:56,190 This is going to be the starting point for our application. 26 00:01:56,190 --> 00:01:59,610 And this is where we're gonna initialize that expressed server. 27 00:01:59,610 --> 00:02:03,120 Now we also have this Mongo D.B. file sitting around. 28 00:02:03,120 --> 00:02:06,600 That's not going to make it into the final version of our application. 29 00:02:06,630 --> 00:02:11,280 You can choose to leave yours around or you can just download the lecture zips if you ever wanted to 30 00:02:11,280 --> 00:02:16,200 remember what we did there and remember that's all documented in EPD f guide as well. 31 00:02:16,380 --> 00:02:22,630 So I'll take a quick moment to delete Mongo D.B. dot J S I'll be kicking things off in index dot and 32 00:02:22,650 --> 00:02:26,730 J asked by loading in express I'm going to require it. 33 00:02:26,730 --> 00:02:33,000 Grabbing the contents like we had done before then I'm gonna go ahead and create the new express application 34 00:02:33,000 --> 00:02:40,050 contest app equals where we call express exactly like we had set up in our web server project from here 35 00:02:40,350 --> 00:02:42,330 we'll go ahead and define the port. 36 00:02:42,330 --> 00:02:45,450 Now eventually we are going to deploy this to Heroku. 37 00:02:45,510 --> 00:02:53,430 So right away I'll get the port value by looking first at process dot EMV dot port which we know is 38 00:02:53,430 --> 00:02:56,980 what we need to do in order to get our app working on Heroku. 39 00:02:57,090 --> 00:03:02,570 Then if that doesn't exist I'll fall back to something I can use for local development like port three 40 00:03:02,570 --> 00:03:03,780 thousand. 41 00:03:03,780 --> 00:03:11,250 Next up all we need to do is use app dot listen right here app dot listen I'm going to listen on port 42 00:03:11,640 --> 00:03:14,700 and then we can provide our message right here. 43 00:03:14,700 --> 00:03:23,490 Something like console dialog server is up on port then I'll go ahead and just concatenate on the port 44 00:03:23,720 --> 00:03:24,390 value. 45 00:03:24,420 --> 00:03:25,410 Perfect. 46 00:03:25,410 --> 00:03:27,670 So we have the basic server in place. 47 00:03:27,720 --> 00:03:31,460 We're also going to set up our package dot Jason scripts. 48 00:03:31,560 --> 00:03:37,500 We want to make sure we can easily use node man and now that it's installed we can set it up in a script. 49 00:03:37,650 --> 00:03:43,620 I'll be removing the test script and we'll be setting up to we're going to set up start. 50 00:03:43,620 --> 00:03:49,320 And we're also going to setup Deb now the dead script is gonna be similar to what we had before. 51 00:03:49,560 --> 00:03:58,110 I'll be using Node Mohn to start up the file in source that is source forward slash index dot J S now 52 00:03:58,110 --> 00:04:00,600 we won't be using handlebars in this project. 53 00:04:00,690 --> 00:04:06,960 So there's no need to provide that extensions argument where we listed out JavaScript files and HB Yes 54 00:04:06,960 --> 00:04:09,540 files this alone is enough. 55 00:04:09,540 --> 00:04:14,820 Now when it comes to Heroku starting our application it'll just use the node command to start up the 56 00:04:14,820 --> 00:04:18,970 same file that is node source index dot J. 57 00:04:18,980 --> 00:04:20,730 S excellent. 58 00:04:20,790 --> 00:04:26,460 So now that we have this in place we can go ahead and start up our server and go through the process 59 00:04:26,460 --> 00:04:30,270 of actually adding our very first root down below. 60 00:04:30,270 --> 00:04:34,700 I'll be running that Dev script NPM run Dev. 61 00:04:34,740 --> 00:04:40,110 Now we should see our message that the server is up and running on port three thousand and it is. 62 00:04:40,110 --> 00:04:45,420 But if we visited it in the browser we wouldn't see anything because we haven't configured the Express 63 00:04:45,450 --> 00:04:48,050 application to actually do anything. 64 00:04:48,090 --> 00:04:53,940 If we were setting up a get request we would use app dot get to configure that that would allow someone 65 00:04:53,940 --> 00:04:58,170 to access the given route using the get h TTP method. 66 00:04:58,170 --> 00:05:02,390 Now in this case we're trying to use post for resource creation. 67 00:05:02,390 --> 00:05:05,240 And for that we use app dot post. 68 00:05:05,240 --> 00:05:11,620 So right here express provides us with methods for all of the T DP methods we're gonna use. 69 00:05:11,660 --> 00:05:18,140 We also have app dot delete and app dot patch which we'll be using as we continue to add endpoints. 70 00:05:18,260 --> 00:05:22,680 Now right here all of those have these same exact call signature. 71 00:05:22,790 --> 00:05:29,510 So the first is still the path and the second is still our callback function which runs when someone 72 00:05:29,510 --> 00:05:31,760 tries to access that particular route. 73 00:05:31,880 --> 00:05:37,970 We get access to the request and the response exactly like we would of if we were using app dot get 74 00:05:38,240 --> 00:05:43,540 inside of that weather web server we create it right here though we're gonna set up the you are out 75 00:05:43,550 --> 00:05:45,890 for this particular post to request. 76 00:05:45,890 --> 00:05:52,830 Now we're creating a new user here so that would be forward slash users plural sized inside of here. 77 00:05:52,880 --> 00:05:57,280 We can now go ahead and just send back a little test response response. 78 00:05:57,380 --> 00:05:59,450 Dot send testing. 79 00:05:59,450 --> 00:06:00,650 Perfect. 80 00:06:00,650 --> 00:06:04,880 Let's go ahead and save this and get this to show up in man before continuing on. 81 00:06:05,480 --> 00:06:11,240 So over inside of post man I'm going to create a brand new collection right here in the collections 82 00:06:11,240 --> 00:06:12,140 menu. 83 00:06:12,200 --> 00:06:15,470 We have one dummy collection for the weather application. 84 00:06:15,470 --> 00:06:18,880 I'll create a another collection for the task app. 85 00:06:18,920 --> 00:06:21,950 Now there's no need to configure anything in these menus. 86 00:06:21,950 --> 00:06:23,720 We'll talk about some of this later. 87 00:06:23,750 --> 00:06:29,510 For now though we can just create that bare bones collection from here we're going to add our first 88 00:06:29,510 --> 00:06:32,360 request into the task app collection. 89 00:06:32,360 --> 00:06:38,660 I can click the little menu I can then click Create task or add request excuse me. 90 00:06:38,660 --> 00:06:43,320 And once we add the request we'll be able to actually configure it right here. 91 00:06:43,340 --> 00:06:45,230 Let's pick a name for this. 92 00:06:45,230 --> 00:06:51,530 I'll use something like create user and down below it's automatically selected the collection right 93 00:06:51,530 --> 00:06:51,860 here. 94 00:06:51,860 --> 00:06:54,350 Task app which is perfect. 95 00:06:54,350 --> 00:06:57,210 Next up we'll save that brand new request. 96 00:06:57,320 --> 00:07:01,920 We will click it which is gonna crack it open in the post man interface. 97 00:07:01,970 --> 00:07:06,320 Now we're gonna go ahead and configure the U.R.L. to what we just set up. 98 00:07:06,320 --> 00:07:08,060 That's local host three thousand. 99 00:07:08,060 --> 00:07:12,400 This is where our server lives forward slash users. 100 00:07:12,390 --> 00:07:18,380 Now when we fire this off with the Get HDTV method we're not going to get anything back. 101 00:07:18,440 --> 00:07:25,220 If I fire that off Express is going to send back a forum for saying that there is no root handler for 102 00:07:25,220 --> 00:07:27,180 what you tried to access. 103 00:07:27,200 --> 00:07:34,630 We explicitly used app dot post which means that we can only access this via the post H TTP method. 104 00:07:34,760 --> 00:07:36,500 Right here I'm going to click it. 105 00:07:36,500 --> 00:07:43,100 Switching from get to post that I'm going to go ahead and send this off when I do that we'll actually 106 00:07:43,100 --> 00:07:45,980 get our testing message showing up. 107 00:07:45,980 --> 00:07:49,740 So at this point we're able to run the given route handler. 108 00:07:49,760 --> 00:07:55,940 The question is how do we provide the data necessary things like the e mail the name the password and 109 00:07:56,000 --> 00:08:02,270 other attributes we might want to send from the client to the server as we saw a couple of videos ago 110 00:08:02,270 --> 00:08:05,690 when we broke down what makes up an HDTV request. 111 00:08:05,690 --> 00:08:10,750 We learned that we'll be sending our Jason data via the request body now. 112 00:08:10,790 --> 00:08:15,110 Postman gives us a way to configure that via the body tab right here. 113 00:08:15,260 --> 00:08:19,750 So I'm gonna switch over from paradigms which is the default to body now. 114 00:08:19,790 --> 00:08:21,880 By default none is selected. 115 00:08:21,980 --> 00:08:28,400 We're gonna switch that over to raw then from the dropdown we'll switch that over from plain text to 116 00:08:28,400 --> 00:08:34,240 Jason allowing us to send Jason data from postman over to the Express server. 117 00:08:34,310 --> 00:08:39,340 This is where we're going to provide everything we need in order to create a new user. 118 00:08:39,440 --> 00:08:43,040 That would be the name the email and the password. 119 00:08:43,040 --> 00:08:48,270 The other attribute age is completely optional and doesn't need to be provided here. 120 00:08:48,320 --> 00:08:50,350 So let's go ahead and get that done. 121 00:08:50,390 --> 00:08:53,090 I will first set up name as Jason. 122 00:08:53,090 --> 00:08:57,080 So remember we have to have our properties in double quotes as well. 123 00:08:57,080 --> 00:08:59,390 I'll just use my name for this. 124 00:08:59,480 --> 00:09:05,400 Then I'll go ahead and set up email and I'll use Andrew and example dot com. 125 00:09:05,990 --> 00:09:11,390 And after that I'll set up the final one we have to provide which is password and I will use something 126 00:09:11,390 --> 00:09:15,020 like read one to three exclamation mark. 127 00:09:15,020 --> 00:09:16,660 Then I'll use the dollar sign. 128 00:09:16,760 --> 00:09:17,780 Perfect. 129 00:09:17,780 --> 00:09:20,650 So now we have our data set up correctly. 130 00:09:20,720 --> 00:09:27,590 And if I click send we're going to see that the data does get sent off and down below we are still getting 131 00:09:27,650 --> 00:09:30,100 our little message and things are going well. 132 00:09:30,110 --> 00:09:36,230 We have our two hundred status code which means that things went OK from here the question is how do 133 00:09:36,230 --> 00:09:40,460 we actually get that data so we can use it to create the new user. 134 00:09:40,460 --> 00:09:42,340 This is a two step process. 135 00:09:42,350 --> 00:09:48,290 And the first is configuring express to automatically pass the incoming Jason for us. 136 00:09:48,410 --> 00:09:50,680 So we have it accessible as an object. 137 00:09:50,690 --> 00:09:58,730 We can easily use we do that by setting up a call to app use to customize our server and express provides 138 00:09:58,730 --> 00:10:00,640 us with something to pass in. 139 00:10:00,710 --> 00:10:01,540 Right here. 140 00:10:01,550 --> 00:10:07,870 We use Express dot Jason calling that as a function when we set up this one line. 141 00:10:07,940 --> 00:10:15,270 It's going to automatically pass incoming Jason to an object so we can access it in our request handlers. 142 00:10:15,290 --> 00:10:19,430 Right here we can do that using a request dot body. 143 00:10:19,430 --> 00:10:24,370 All I'm gonna do for the moment is dump that into the console so we can look at exactly what we get. 144 00:10:24,530 --> 00:10:27,450 Right here I'll dump a request dot body. 145 00:10:27,590 --> 00:10:34,100 I'm gonna save the file so no demand restarts and I'll fire off the exact same post command request 146 00:10:34,280 --> 00:10:36,300 from the post man application. 147 00:10:36,320 --> 00:10:42,110 Now I'm not getting anything new in the response which is OK but over in the terminal in visual studio 148 00:10:42,110 --> 00:10:43,980 code I should see my data. 149 00:10:44,000 --> 00:10:45,650 And right here I have it. 150 00:10:45,650 --> 00:10:51,230 I have the name email and password exactly as they were provided in postman. 151 00:10:51,230 --> 00:10:57,860 So now we know how to grab the incoming body data and we can use it to actually create a new user to 152 00:10:57,860 --> 00:10:58,670 get that done. 153 00:10:58,670 --> 00:11:04,670 We have to make sure that Mongoose connects to the database and we need to get access to our user model 154 00:11:04,700 --> 00:11:10,910 from inside of this file that's going to require us to do a little bit of restructuring as to what lives 155 00:11:10,910 --> 00:11:14,690 inside of Mongoose dot J S in here. 156 00:11:14,690 --> 00:11:18,680 We're just going to have the code necessary to connect to the database. 157 00:11:18,680 --> 00:11:25,220 We're going to move our models to other files for that let's create a new directory in the source folder 158 00:11:25,220 --> 00:11:26,660 called models. 159 00:11:26,660 --> 00:11:33,140 This is going to sit alongside of the D directory and in here we'll have a single file for each of the 160 00:11:33,140 --> 00:11:34,960 models we want to create. 161 00:11:35,030 --> 00:11:41,360 Right now we'll have just one which I'll call user dot J S and in the next video you'll be breaking 162 00:11:41,360 --> 00:11:44,260 out the task model in much the same way. 163 00:11:44,300 --> 00:11:47,330 So what's going to go inside of user dot J S. 164 00:11:47,390 --> 00:11:54,050 Well the entire user model from the const declaration all the way down to the end after we define all 165 00:11:54,050 --> 00:12:00,380 of the attributes I'm gonna take that I'm going to cut it out of its current location and I'll bring 166 00:12:00,380 --> 00:12:09,110 it over to user dot J S now inside of here we are indeed using Mongoose and we are also using that validator 167 00:12:09,110 --> 00:12:09,920 library. 168 00:12:09,950 --> 00:12:17,200 So both of those needed to get loaded in as well over inside of Mongoose dot J S I can simply grab the 169 00:12:17,210 --> 00:12:21,890 two existing require lines and I can bring them over to the new file. 170 00:12:21,920 --> 00:12:27,230 Now we can go ahead and make sure that we actually export something from this file down below. 171 00:12:27,230 --> 00:12:33,980 We want to export user so other files like index dot J S can use it to create new users. 172 00:12:34,070 --> 00:12:41,600 Way down at the bottom of the file we'll get this done by setting module down exports equal to uppercase 173 00:12:41,720 --> 00:12:42,980 U user. 174 00:12:43,070 --> 00:12:44,150 Perfect. 175 00:12:44,150 --> 00:12:50,960 Now we're done with this file and we can go ahead and save it over in Mongoose dot J S we have a little 176 00:12:50,960 --> 00:12:58,720 extra code validator is no longer being used in this file so I can go ahead and remove the require call. 177 00:12:58,820 --> 00:13:04,880 I also have some example code where we learned how to create new instances of our models and save them. 178 00:13:04,880 --> 00:13:11,570 I'm going to remove that for user and I'm also going to remove that for a task down below for the moment 179 00:13:11,570 --> 00:13:17,180 will leave the task model exactly where it is as we're not going to touch that in this video. 180 00:13:17,180 --> 00:13:19,000 So now what do we need to do. 181 00:13:19,010 --> 00:13:21,530 Well we need to head back to index dot J. 182 00:13:21,540 --> 00:13:28,640 S and load a couple of things in the first thing I want to do is require the Mongoose file. 183 00:13:28,760 --> 00:13:35,780 So from index dot J S that would be dot forward slash D.B. forward slash mongoose. 184 00:13:35,780 --> 00:13:40,490 Now we don't want to actually grab anything from that file by simply calling require. 185 00:13:40,490 --> 00:13:47,230 That's going to ensure that the file runs and it's going to ensure that Mongoose connects to the database. 186 00:13:47,270 --> 00:13:55,100 The next thing we're going to do in index dot J S is actually load user in right here const user equals 187 00:13:55,340 --> 00:14:01,520 a another call to require dot forward slash models forward slash user. 188 00:14:01,520 --> 00:14:02,740 Excellent. 189 00:14:02,750 --> 00:14:09,260 Now with this in place we can create a new instance of the user inside of our route handler and we can 190 00:14:09,260 --> 00:14:11,550 make sure that they get saved correctly. 191 00:14:11,570 --> 00:14:14,330 The first thing we're gonna do is create a new user. 192 00:14:14,360 --> 00:14:17,660 So const user equals a new user. 193 00:14:17,660 --> 00:14:23,450 Now we know the constructor function for our models requires us to pass in an object with all of the 194 00:14:23,450 --> 00:14:29,390 attributes we want to set up and in our case we already have an object that has all of those attributes 195 00:14:29,660 --> 00:14:33,680 as we just saw that is request dot body. 196 00:14:33,680 --> 00:14:39,890 Now from here we can actually go ahead and call user dot save to attempt to save the new user. 197 00:14:39,890 --> 00:14:41,480 It might work it might not. 198 00:14:41,480 --> 00:14:43,590 Either way we want to give it a try. 199 00:14:43,610 --> 00:14:51,050 Down below I'll setup a then call to handle successes and I'll set up a catch call to handle errors. 200 00:14:51,050 --> 00:14:58,010 So dot then with a callback function then dot catch with a callback function and in both we're going 201 00:14:58,010 --> 00:15:02,210 to respond to the individual who made the request. 202 00:15:02,280 --> 00:15:08,020 Now when things go well we're just going to send back the user with all of the additional fields. 203 00:15:08,070 --> 00:15:13,930 The Age has been set to its default and an idea has been generated for us right here. 204 00:15:13,980 --> 00:15:16,970 We'll use response dot send to get that done. 205 00:15:16,980 --> 00:15:18,390 What are we going to send back. 206 00:15:18,390 --> 00:15:20,610 I'm going to send back the user. 207 00:15:20,610 --> 00:15:21,190 Perfect. 208 00:15:21,630 --> 00:15:25,470 Let's go ahead and test this out before actually going any further. 209 00:15:25,500 --> 00:15:29,820 Now in order for this code to run the data we send across needs to be valid. 210 00:15:29,820 --> 00:15:31,850 This data is indeed valid. 211 00:15:31,860 --> 00:15:34,470 We can go ahead and click that send button. 212 00:15:34,590 --> 00:15:38,820 And right here I can see that I have the entire model showing up. 213 00:15:38,820 --> 00:15:44,660 I have all of the attributes we provided including the additional ones that were generated for us. 214 00:15:44,700 --> 00:15:48,960 Now the new user we created was actually saved to the database. 215 00:15:48,960 --> 00:15:55,530 I can prove that by heading over to robo 3 T from here we have our users collection. 216 00:15:55,530 --> 00:15:57,660 I'm going to go ahead and refresh it. 217 00:15:57,780 --> 00:16:02,590 I'll expand to the last document and this is exactly what we just created. 218 00:16:02,610 --> 00:16:03,720 So there we go. 219 00:16:03,720 --> 00:16:06,560 That is our very first rest api. 220 00:16:06,660 --> 00:16:13,950 We're able to send data via an H TTP request off to the Express server using a predefined operation 221 00:16:14,130 --> 00:16:17,760 we're able to perform some sort of manipulation on the database. 222 00:16:17,760 --> 00:16:20,020 In this case creating the user. 223 00:16:20,070 --> 00:16:23,040 Now we still want to handle errors as well. 224 00:16:23,040 --> 00:16:28,770 If something goes wrong we will get access to the error and we'll be able to send that as part of their 225 00:16:28,770 --> 00:16:29,800 response. 226 00:16:29,850 --> 00:16:36,490 To do that we'll use response dot send sending back the error just like this now. 227 00:16:36,540 --> 00:16:41,460 When it comes to sending back errors we also want to change the status code. 228 00:16:41,460 --> 00:16:44,040 So let's go ahead and view the current output. 229 00:16:44,040 --> 00:16:45,770 If we generated an error. 230 00:16:45,990 --> 00:16:49,080 What I'm going to do is provide a password that's too short. 231 00:16:49,140 --> 00:16:54,900 I'll get rid of a bunch of characters leaving just two characters as part of the password and if I go 232 00:16:54,900 --> 00:16:57,750 ahead and send this off I would expect it to fail. 233 00:16:57,900 --> 00:17:01,030 Right down below we are indeed getting the message. 234 00:17:01,080 --> 00:17:07,490 The path password with the value R E is shorter than the minimum allowed length which is 7. 235 00:17:07,500 --> 00:17:13,980 So right here that is indeed a problem and we're getting the error sent back and no new user has been 236 00:17:13,980 --> 00:17:15,120 created. 237 00:17:15,120 --> 00:17:20,520 Now these status code is still a two hundred which is a bit misleading when we're working with rest 238 00:17:20,550 --> 00:17:27,000 api eyes we want to provide the most accurate status codes possible for a complete list of all of the 239 00:17:27,000 --> 00:17:28,670 status codes we can work with. 240 00:17:28,980 --> 00:17:33,400 We can head over to the browser and go to a really neat little Web site. 241 00:17:33,480 --> 00:17:38,110 It is h TTP statuses dot com. 242 00:17:38,130 --> 00:17:42,710 Here we can view a complete list of all of the common status codes we can provide. 243 00:17:42,720 --> 00:17:46,770 Now our HDTV status codes are broken into different groups. 244 00:17:46,770 --> 00:17:48,230 They're all in the hundreds. 245 00:17:48,230 --> 00:17:52,020 We have our one hundred status codes which are informational. 246 00:17:52,080 --> 00:17:55,380 Our two hundred status codes which are for success. 247 00:17:55,470 --> 00:18:02,190 Three hundred four redirection 404 client error and a five hundred for server error. 248 00:18:02,190 --> 00:18:08,760 Now for our rest api we'll be using two hundred status codes when things go well such as when you provide 249 00:18:08,790 --> 00:18:12,520 user data to create a user and we're able to create it. 250 00:18:12,660 --> 00:18:18,630 We'll be using four hundred status codes for when things go wrong because of something the client did 251 00:18:18,840 --> 00:18:25,650 such as providing invalid data and we'll be using five hundred status codes when something goes wrong 252 00:18:25,680 --> 00:18:27,930 because of an error on the server. 253 00:18:27,930 --> 00:18:35,310 So in this case for an invalid password or any invalid user data we want to use a four hundred status 254 00:18:35,310 --> 00:18:41,130 code and we're gonna go ahead and use four hundred for each of these status codes you can actually click 255 00:18:41,130 --> 00:18:42,860 on it to learn more about it. 256 00:18:42,870 --> 00:18:48,270 So right here each of them have an individual page now to set up these status code. 257 00:18:48,270 --> 00:18:51,270 We use the status method on the response. 258 00:18:51,300 --> 00:18:57,860 So right here I can use response dot status to set the status equal to four hundred. 259 00:18:57,870 --> 00:19:02,490 Now it's important that we do this before we call response dot send. 260 00:19:02,520 --> 00:19:06,050 Otherwise the status isn't gonna get sent correctly. 261 00:19:06,090 --> 00:19:11,610 Now let's go ahead and move back over to postmen and fire off that request one more time with the broken 262 00:19:11,610 --> 00:19:12,560 password. 263 00:19:12,630 --> 00:19:17,950 Right here we can see the status was changed to four hundred which is fantastic. 264 00:19:18,000 --> 00:19:20,990 Now we don't have to use two statements to get that done. 265 00:19:21,030 --> 00:19:29,130 We can chain together our method calls so I can use response dot status dot send to send back the error 266 00:19:29,390 --> 00:19:31,840 and that makes this other line irrelevant. 267 00:19:31,860 --> 00:19:38,340 So right here we have the exact same setup using a single line and we'll be taking advantage of chaining 268 00:19:38,400 --> 00:19:44,700 when it's available right here in postman we can send this off for the final time and we get the exact 269 00:19:44,700 --> 00:19:45,740 same result. 270 00:19:45,840 --> 00:19:51,240 We're still getting our response body which is good and we're still seeing our new status code. 271 00:19:51,240 --> 00:19:53,650 Now that's where I'd like to stop before this one. 272 00:19:53,670 --> 00:20:01,760 In this video we set up our very first rest api route the route for creating a new user we saw how we 273 00:20:01,760 --> 00:20:08,480 could refactor the Mongoose file to focus on just connecting to the database and we now defined the 274 00:20:08,480 --> 00:20:12,010 model user in a separate file down below. 275 00:20:12,050 --> 00:20:16,220 We use it inside of our root handler in the next video. 276 00:20:16,250 --> 00:20:22,480 You're going to do something similar but for tasks you'll be setting up a new route for creating a task. 277 00:20:22,490 --> 00:20:27,170 I'm really excited to get to that challenge and give you some more hands on experience. 278 00:20:27,170 --> 00:20:28,700 So let's jump right into that.