1 00:00:00,500 --> 00:00:03,991 Express is referred to as a routing and middle way framework. 2 00:00:03,991 --> 00:00:07,620 Routing lets our app do different things and display different information, 3 00:00:07,620 --> 00:00:12,310 based on which you are at the browser requests, and how it requests it. 4 00:00:12,310 --> 00:00:17,130 So sending a GET request to the slash login route leads to the login form. 5 00:00:17,130 --> 00:00:21,610 While a PUT request to slash login, tells the app to authenticate the user 6 00:00:21,610 --> 00:00:24,330 by comparing the form data against records in the database. 7 00:00:25,780 --> 00:00:30,110 Middleware is software that sits in the middle of other parts of your application. 8 00:00:30,110 --> 00:00:34,450 In other words, it's programming that runs after a request is received, but 9 00:00:34,450 --> 00:00:36,790 before a response is sent back. 10 00:00:36,790 --> 00:00:38,780 We've already seen a few examples of middleware. 11 00:00:39,880 --> 00:00:44,580 For instance, most express apps ours included use the body parser module 12 00:00:44,580 --> 00:00:48,200 to convert incoming requests into a format that's easy for 13 00:00:48,200 --> 00:00:49,880 a JavaScript program to use. 14 00:00:50,890 --> 00:00:53,210 The Express app gets a request. 15 00:00:53,210 --> 00:00:57,690 Body parser takes the body of the request, makes it readable to our program 16 00:00:57,690 --> 00:01:01,190 then hands it off to the next part of the Express application. 17 00:01:01,190 --> 00:01:04,000 It sits in the middle of that request cycle. 18 00:01:04,000 --> 00:01:08,200 Another example of middleware in our application is the Express session module 19 00:01:08,200 --> 00:01:10,580 we used to track sessions for logged in users. 20 00:01:11,620 --> 00:01:14,180 This is called application level middleware, 21 00:01:14,180 --> 00:01:17,640 because it makes sessions available anywhere in our app. 22 00:01:17,640 --> 00:01:20,010 You'll often use more than one piece of middleware. 23 00:01:20,010 --> 00:01:23,890 In fact, you can chain together multiple middleware functions 24 00:01:23,890 --> 00:01:25,610 that run one after the other. 25 00:01:26,880 --> 00:01:30,980 In Express, middleware is a function that has access to the request and 26 00:01:30,980 --> 00:01:32,450 response objects. 27 00:01:32,450 --> 00:01:35,850 The entire process from request to response in Express 28 00:01:35,850 --> 00:01:38,340 is called the request response cycle. 29 00:01:38,340 --> 00:01:42,600 A typical piece of middleware is a function that accepts three arguments, 30 00:01:42,600 --> 00:01:48,380 the request object, the response object and a third parameter named next. 31 00:01:48,380 --> 00:01:51,860 Next is a function, and it represents the next piece of middleware, 32 00:01:51,860 --> 00:01:54,188 the next function in the request response cycle. 33 00:01:54,188 --> 00:01:58,340 In our app, we've added a bit of middleware to make sure that the user ID 34 00:01:58,340 --> 00:02:02,010 property of the session is available to the application. 35 00:02:02,010 --> 00:02:06,290 This middleware function assigns a user ID to the current user property. 36 00:02:06,290 --> 00:02:06,990 When it's done, 37 00:02:06,990 --> 00:02:11,990 it calls next, telling express to move on to the next piece of middleware. 38 00:02:11,990 --> 00:02:15,730 When writing custom middleware, we must always include a call to next. 39 00:02:18,420 --> 00:02:22,500 Let's take a closer look at the middleware we're already using in our app. 40 00:02:22,500 --> 00:02:28,410 If you look in the app.js file, you can see multiple app.u statements. 41 00:02:28,410 --> 00:02:30,870 These add middleware to the application. 42 00:02:30,870 --> 00:02:36,043 Some are third party middleware, like session and body parser, and 43 00:02:36,043 --> 00:02:41,218 some are built into Express like Express.static which lets a sort of 44 00:02:41,218 --> 00:02:46,587 static files within a specific directory, like images and CSS files. 45 00:02:48,247 --> 00:02:51,691 Also, towards the bottom of the file, there's error handling 46 00:02:51,691 --> 00:02:56,800 middleware that defines middleware function in line as an anonymous function. 47 00:02:56,800 --> 00:03:00,090 It accepts an error object and serves that to the user. 48 00:03:00,090 --> 00:03:03,820 You can see that the error handling middleware takes four parameters, 49 00:03:03,820 --> 00:03:08,590 the usual request, response, and next parameters but 50 00:03:08,590 --> 00:03:10,760 the first argument is an error object. 51 00:03:12,260 --> 00:03:14,050 There's also router middleware. 52 00:03:14,050 --> 00:03:16,720 That's a function you add to particular routes. 53 00:03:16,720 --> 00:03:19,680 You can find links to more information about Express middleware 54 00:03:19,680 --> 00:03:21,110 in the teachers notes for this video. 55 00:03:22,970 --> 00:03:26,620 Next, we'll write custom middleware for our user authentication system. 56 00:03:26,620 --> 00:03:29,950 Specifically, we'll create router middleware to redirect visitors 57 00:03:29,950 --> 00:03:31,290 based on their logon status.