1 00:00:00,340 --> 00:00:05,510 A 404 signals that the user requested a route that doesn't exist. 2 00:00:05,510 --> 00:00:08,670 Remember, when an app gets a request, 3 00:00:08,670 --> 00:00:12,880 it will go from one app.use call to the next looking for a match. 4 00:00:13,880 --> 00:00:19,808 If it gets to the end without finding a route and there are no errors, 5 00:00:19,808 --> 00:00:26,220 Express' native handler will send a 404 back to the client with some plain text. 6 00:00:26,220 --> 00:00:30,120 If we catch the request before it gets to the end of the line, 7 00:00:30,120 --> 00:00:32,180 we can send users a better page. 8 00:00:33,540 --> 00:00:37,960 Check out Treehouse's 404 page, or Lego's, 9 00:00:39,470 --> 00:00:42,410 or my personal favorite, GitHub. 10 00:00:42,410 --> 00:00:46,130 We can give our users a less scary page too. 11 00:00:46,130 --> 00:00:50,250 Let's write some middleware to handle 404 errors in our app. 12 00:00:51,560 --> 00:00:58,660 We want to access the request at the end of our app and before the error handler. 13 00:00:58,660 --> 00:01:03,104 So let's call app.use here. 14 00:01:10,033 --> 00:01:13,549 We're going to use the error handler we wrote in the last video 15 00:01:13,549 --> 00:01:15,180 to handle the actual error. 16 00:01:16,740 --> 00:01:19,080 This middleware will just be responsible for 17 00:01:19,080 --> 00:01:23,310 creating the error object and handing off to the error handler. 18 00:01:25,135 --> 00:01:33,116 I'll create a new error, Not found. 19 00:01:36,770 --> 00:01:39,988 Then I'll set the error status code, 20 00:01:42,312 --> 00:01:49,025 To 404, Before parsing it to the next function. 21 00:01:53,250 --> 00:01:58,866 Now, any requests that makes it this far will run this function and 22 00:01:58,866 --> 00:02:01,280 trigger the error handler. 23 00:02:01,280 --> 00:02:06,430 The error handler itself will send the page out to the user in that event. 24 00:02:06,430 --> 00:02:08,090 Let's try it out. 25 00:02:08,090 --> 00:02:10,942 In the browser, I'll request a route that doesn't exist. 26 00:02:16,040 --> 00:02:22,427 Voila, now when our users go astray, they will get a friendlier notice. 27 00:02:22,427 --> 00:02:25,982 We've covered the basics of Express now, well done. 28 00:02:25,982 --> 00:02:28,922 Let's cement your knowledge in the remainder of this course by 29 00:02:28,922 --> 00:02:30,814 finishing our flash card application. 30 00:02:30,814 --> 00:02:33,290 Are you ready to apply your knowledge? 31 00:02:33,290 --> 00:02:33,790 See you there.