Play with middleware to see how it works.
Let's write some Middleware right into our app to see how it works. 0:00 I'll just make some space above our routes on line 12. 0:05 You'll often pass Middleware is an anonymous function into the app use 0:11 method. 0:16 We need the request, response, and next arguments. 0:18 Let's just do a log statement to the console. 0:26 Then we can call next. 0:35 We'll look more closely at the next function sin, but for 0:38 now, it is enough to know that we're passing control forward through the app. 0:41 You can think of next as the next step on the conveyor belt. 0:47 This middleware runs every time a request comes into the app. 0:52 Switch over to the browser, and refresh the page. 0:58 Nothing changes on the page, but if we look in the console, 1:04 you will see our message is logged. 1:08 Now we'll add another middleware function. 1:13 Just copy this below and change the message. 1:16 When I save and refresh the browser, the terminal shows 1:22 two messages in the order which they appear in the code. 1:28 It's important to know that you can depend on the sequence. 1:33 Earlier middleware functions, runs before the later ones. 1:37 You can parse several functions into the same app.use method code and 1:41 they will run in the order they're parsed in. 1:46 Let's add another function to the first app.use. 1:50 We'll copy the function, put a comma, and then paste. 1:54 This will log between one and two. 2:02 So let's say one and a half. 2:05 Save, refresh, and check the console. 2:10 Each middleware function printed in the order we expected. 2:14 So we've just seen two ways to include middleware functions in the app. 2:20 You can call the app.use as many times as you'd like, 2:25 passing in a function to each call. 2:29 You can also parse in many functions as you want into a single app.use call. 2:32 Express will always run functions in the order they appear. 2:38 Let's trigger the middle word based on the URL or route that a user visits. 2:42 We can do this by placing the URL as the first argument to the app.use method. 2:47 Let's wrap the first app.use, two slash one. 2:54 Refreshing the browser, 3:03 we see that the terminal only showed the string two logged. 3:04 That's because this middleware runs with every request no matter what. 3:10 If I clear the console and go to /one in the browser, You see an error. 3:16 As we've seen, that just means we haven't set up a route handler for this URL. 3:25 But what we're really interested in, is the console at this point. 3:30 Switching back to the console, we have One, One and a half, and Two. 3:34 You can also pass information between functions. 3:41 I'll delete these routes, And 3:44 these second middleware arguments for now. 3:49 Let's use the request object to pass the dates from the first middleware function 3:54 to the next middleware function. 3:58 In the first function, we'll need to create a property called message. 4:01 Let's assign the string 'This message made it!'. 4:07 Message isn't a special name. 4:15 I could call it anything or add more properties if I wanted. 4:18 The idea here Is that we'll use the property to pass 4:22 as message from one middleware function to the next. 4:26 In the next middleware function I can read the message back 4:30 from the property and log it out. 4:35 Let me check to see if it works. 4:42 And it does. 4:45 We just modified the request object for the first time. 4:47 That's a lot of what gives middleware its power. 4:51 Allowing us to gather and compute data and send it back to the client. 4:54 BodyParser for example, creates a body property on the request object, 4:58 and then we can use that to respond to the client. 5:03 Let's take a closer look at the next function in the next video. 5:06
You need to sign up for Treehouse in order to download course files.Sign up