An Express application is similar to a conveyor belt receiving requests at one end, and sending responses from the other. This video shows how this looks in terms of code. The `use` method accepts a function as a parameter. This function is middleware.
You can think of the express app up as a conveyor belt. 0:00 That receives requests at one end and sends out responses at the other. 0:03 All the work along the conveyor belt is done by what is called Middleware. 0:08 The request and response objects move along the belt, and 0:13 each piece of middleware picks them up. 0:16 Either of the objects can be augmented in some way. 0:19 The last piece of middleware can be the route handler, the end of the line. 0:22 The response that was built is sent back to the client. 0:27 Let's see what this looks like in terms of code. 0:32 Our app object registers middleware with the use method. 0:35 The use method accepts a function as a parameter. 0:40 This function is middleware. 0:43 When Express calls the function, it passes in three arguments. 0:45 The first two are the request object, and the response object. 0:50 The request object is what's coming in from the user of the app. 0:54 The response object is what the app sends back. 0:59 The final argument next, is a function that must be called by the middleware. 1:02 Once the work has been finished. 1:07 The middleware is saying next, and 1:09 handing control over to the next piece of middleware. 1:12 If nextt is never called, the request response cycle will not finish. 1:15 The app.use method applies middleware to all routes. 1:20 Any requests to the server for example slash help, slash sign up or 1:25 slash logging will run through the same middleware function. 1:29 If you wish the middleware to only work on a certain route. 1:34 You can enter the route as the fast parameter. 1:37 Let's write some example middleware in our app. 1:41 We're going to use a Google Chrome application called Postman. 1:44 Postman is a great tool for testing rest API. 1:47 See the teachers notes for instructions on how to set it up. 1:51 I have the app.js file open in my text editor, right under the app declaration. 1:55 Let's put our first piece of middleware, app.use. 2:02 The handler function. 2:08 With the request, response and 2:13 next parameters. 2:21 Console.log, and the text. 2:25 First piece of middleware. 2:32 And then the next call. 2:42 I'm going to copy this piece of middleware, and paste it underneath. 2:49 Replacing first with second. 2:55 Switch over to the terminal and restart the server by pressing Ctrl+C. 3:00 Stop it and start it over again. 3:07 Now let's make a GET request in our app using Postman. 3:12 Type in the URL local host column 3,000. 3:18 Choose GET from the drop down and hit send. 3:22 You see we get a error, because we haven't set up a way for 3:29 our app to respond to clients yet. 3:32 We're really just interested in the logging our middleware 3:35 is doing in the console. 3:38 Switch back to the terminal, and 3:40 you can see that it logged our messages when we issued an HTTP request to our app. 3:42 We can see from these messages that the middleware was triggered in order. 3:49 Now let's change the second piece of middleware. 3:57 This time we're adding a route as the first parameter 4:00 Moving to the terminal, restart the server. 4:12 And if we send the same URL in Postman again and we look at the console. 4:19 We see only the first middleware is run. 4:26 Let's hit return in our console a couple of times to give a space 4:29 to see the next console output. 4:33 If we head back to Postman and 4:36 enter slash different, hit Submit. 4:40 And let's look at the logs this time. 4:46 Both of them ran. 4:51 Because the first runs with every request, 4:54 while the second only runs when we request the URL specified in its routes. 4:57 Now let's go a step further and 5:05 tack a parameter on to the end of the different route. 5:07 Let's modify our log to use the request object to access the param and 5:14 the ID for our route. 5:19 Whatever the parameter is in the route, becomes a property on the request params. 5:36 The colon is only there for express to know what we want to use as a param. 5:42 Save this. 5:50 Stop the server. 5:52 Clear the screen with clear, and start again. 5:54 In Postman, send a request to localhost:3000/different/42. 6:00 Hit Send. 6:09 Back in the terminal, we see we're able to access the URL parameter. 6:11 Let's try something a little more interesting by passing values 6:16 between middleware. 6:21 First, delete the route specifier. 6:22 Now, let's change the middleware functions. 6:30 In the first middleware, 6:36 we're going to add a new property to the request object called my message. 6:38 Hello, middleware two. 6:47 Then we can access it in our second middleware. 6:54 This is often how access to objects or data is moved down the line in Express. 7:04 They are set as properties on the request or response objects. 7:10 Restart the server and make a request to localhost 3000 again. 7:19 We see in the terminal that the second middleware successfully received and 7:35 printed the message. 7:40 Let's make one more example to demonstrate one of the properties Express 7:41 adds to the request object. 7:46 Query, or query parameters. 7:48 Let's log out. 7:57 The leaves on the trees are. 8:02 If you want to obtain a query parameter from a query string. 8:08 You can access it through the query objects on the request. 8:12 Each of the query paraameters are set as values on the query object. 8:21 In this case, we want to get the color query parameter. 8:26 To reinstate, the query property 8:30 is the way that express gives us access to the URLs query string. 8:34 In this case, where X is in the query string parameter of color. 8:38 Let's see what this URL looks like. 8:43 Restart the server. 8:47 Then in Postman, we'll make a GET request to a localhost:3000/?. 8:52 Color is equal green. 8:59 Looking in the terminal. 9:06 We see that we're able to use the query 9:07 strings values that are stored in the request objects. 9:10 Rest APIs use query strings for GET routes. 9:14 For things like searching and filtering resources. 9:17 For now, let's clean this experiment off. 9:21 And in the next video, we'll add middleware to handle data in our app 9:24 via the post and put HTTP methods. 9:29
You need to sign up for Treehouse in order to download course files.Sign up