Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
When you're building out an express application, usually you're handling one request at a time. However, there's some instances where there's logic applying to all requests. In this video, we'll write some Express middle ware for all requests. Go to the Exercise Files and go to chapter five, video five and then copy the Start folder to the Desktop. Now let's go to terminal and install the modules. Type in cd and space and then I'm just going to drag the folder into terminal to get this path.
Now I'm going to type npm space install and then enter. Now all the modules are installed and we're ready to go. Let's start up the server and then go to one of our routes. Type in node, space, app. And then hit Return. The server is now listening on port 3000. I'm going to go to Postman. If you don't already have Postman installed, you can get it from the Google Chrome store. We're going to be looking at some HTTP Header information. And that information can also be found in many of the web inspectors with different web browsers.
So you don't necessarily have to use Postman, but I find this one to be the most convenient. There's already a URL entered here and I just want the flight 33 route. And I'm going to switch to the Get method and now I'm going to send this request. Now click on over to the Headers tab. One thing you'll notice, is a Header here called X-Powered-By and the value is currently set to Express. This is a default value that Express sends out, but I'd really like to change this to something else, because I don't necessarily want to broadcast to the world that I'm using the Express framework.
So to do that, we're going to write a small piece of middleware, that'll change this value. Open up the Start folder in Sublime Text or any Text Editor you prefer and then go to App.js. We're going to add the middleware right here in the middle of all these app.use calls. We're going to add our own. So type app.use and then as the argument you're going to pass in a function. This function is going to provide three arguments, request, response and next.
So now I'm going to reset that X-Power-By Header. We're going to type res.set and then the first argument is the Header name. The second argument is the value that we want to assign to that Header. I'm going to call this Flight Tracker. The function I've just written is known as middle ware. It's designed so you can easily add it into your express application. The arguments are important to note, just like HTTP requests, you get the request and response objects passed as arguments.
However, the final argument is a function called next. It's very important to call next. Otherwise the server will never return a response. Calling next tells Express to process the next piece of middleware. So now let's save this file and then go back to terminal. Now, we're currently still running the same code that we started with. And we need to stop the server, so that it reloads. So press ctrl + c and then press up on the keyboard. This will bring up the last Command. And now finally press Return.
And now the server is been restarted. Let's go back to the browser and then click Send again. You'll notice that this response is taking awhile to come back. We may not of coded this quite correctly. I'm going to stop the server and I'm going to go back to the code. So here's the problem with the code. We never called that next function, so it stopped here on line 22. Let's add a line beneath that and call next. Not let's go back to terminal and restart the server.
And now let's go back to the browser and try the request again. So now if we go to the Headers, X-Powered-By is now set to FlightTracker. This should also work for other requests. Let's pull up that put request from the previous video. I'm going to Send it and then the Headers still say Flight Tracker. It should also even work if we do an invalid request. For instance, if I change this URL to info and hit Send, it'll tell me that it can not put info. But if I go to Headers, it'll still show Flight Tracker for X-Powered-By.
When you need to make a change to every request reaching your express application, write middleware. Just be sure to call the next function, otherwise other middleware will never run. In the next video, we'll create some templates for get requests.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97456 Viewers
61 Video lessons · 84687 Viewers
71 Video lessons · 68887 Viewers
56 Video lessons · 101331 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.