1 00:00:00,000 --> 00:00:04,730 [MUSIC] 2 00:00:04,730 --> 00:00:08,820 We already used some Express Middleware in our project. 3 00:00:08,820 --> 00:00:12,580 We use the body parser to post incoming form data and 4 00:00:12,580 --> 00:00:14,980 the cookie parser to read cookie information. 5 00:00:16,020 --> 00:00:18,300 But that's not all you can do with Middleware. 6 00:00:18,300 --> 00:00:21,130 In fact, Middleware is so integral to Express, 7 00:00:21,130 --> 00:00:24,350 just about everything you write in Express is Middleware. 8 00:00:24,350 --> 00:00:25,920 So what is it? 9 00:00:25,920 --> 00:00:30,630 Consider when someone asks you a question, you hear the question, think about it for 10 00:00:30,630 --> 00:00:33,640 a moment, and then answer the question. 11 00:00:33,640 --> 00:00:37,750 If we relate this to the request response cycle, that middle step, 12 00:00:37,750 --> 00:00:42,080 the thinking, is analogous to what Middleware does. 13 00:00:42,080 --> 00:00:46,410 An Express application receives requests and sends responses. 14 00:00:46,410 --> 00:00:51,580 You can sort of think of Express' request response cycle like a conveyor belt. 15 00:00:51,580 --> 00:00:55,750 The result comes in at the beginning and the response leaves at the end. 16 00:00:55,750 --> 00:00:59,495 All along the conveyor belt Middleware acts upon the requests, 17 00:00:59,495 --> 00:01:03,390 packaging up a response to send back to the end user. 18 00:01:03,390 --> 00:01:07,300 There can be as many pieces of Middleware as you want to have before sending 19 00:01:07,300 --> 00:01:08,080 a response. 20 00:01:09,250 --> 00:01:12,340 The basic structure of Middleware code is very simple. 21 00:01:13,350 --> 00:01:19,050 It's a function with three parameters, request, response and next. 22 00:01:19,050 --> 00:01:22,430 Inside the function, Middleware can read and modify the request and 23 00:01:22,430 --> 00:01:23,920 response objects. 24 00:01:23,920 --> 00:01:27,860 For example, the cookie parser modified the response object, 25 00:01:27,860 --> 00:01:31,010 placing the cookie's contents onto it. 26 00:01:31,010 --> 00:01:34,930 Next is a function that must be called, when the work is done. 27 00:01:34,930 --> 00:01:39,930 This triggers the Middleware function after the current one to execute. 28 00:01:39,930 --> 00:01:45,040 To run Middleware in response to requests, pass it into app.use. 29 00:01:45,040 --> 00:01:48,600 This will run the Middleware function for every request. 30 00:01:48,600 --> 00:01:49,790 To only run it for 31 00:01:49,790 --> 00:01:54,990 a specific route, pass the router argument in before the Middleware function. 32 00:01:56,000 --> 00:02:00,050 You can also limit the Middleware to only use get requests. 33 00:02:01,090 --> 00:02:04,970 This is done by using get instead of use. 34 00:02:04,970 --> 00:02:07,200 Hopefully this is looking familiar. 35 00:02:07,200 --> 00:02:11,300 This is very similar code to what we've been running to handle our routes. 36 00:02:11,300 --> 00:02:15,950 The only difference is that we haven't been declaring or using next yet, but 37 00:02:15,950 --> 00:02:16,960 we'll get to that soon. 38 00:02:18,130 --> 00:02:21,360 In the next video, let's write some Middleware to get a better feel for 39 00:02:21,360 --> 00:02:22,390 how it works.