The app.use method in express.js
When making an express.js application one of the most important methods in the app object is the app use method. This method is important for making use of middleware modules, as well making your own middle ware methods. I will not be getting into middleware in depth here though but I have a post that is a good starting point for express middleware. There is also the app all method that does work in a similar way to that of app.use, but they are not the same thing.
1 - Express app use and what to know for starters
This is a post on a certain methods in an instance of an app object in the node.js powered framework known as expressjs. It is not a getting started post on express.js, javaScript, node.js or any additional skill required before hand. If you are new to express you might want to start at my express.js mega post, or my post on getting started with express.
2 - An example of the express app use method and custom middleware
For a basic example of app.use I came up with a quick demo that just sets a property called useClient in the request object. In a more advanced project this might be used as a means to set what client system to use for certain browsers.
2.1 - Setup the demo folder
To start things off just like any other express demo I create a new folder, and cd into it to make it the current working folder. Once that is done I use npm init to set up a new package.json file for the demo, and then install express as one of the dependencies.
|
|
When making this demo I was using express 4.16.3, but if you aim to make an actual production app chances are you will want to always use the latest version.
2.2 - The set-client.js file
In this file I will be using the app.use method to define what will be done with incoming requests. It will look for a user agent header in the request headers using req.get. Base on what is there it may set a value that will eb appended to the request object to something other than what it is by default. In any case it will then call next to continue on with the normal flow of things.
|
|
It is also possible to have it so it will look at query strings, or set the value by another means, but you get the idea. If I had a project in which there was more than one client system this can work as a way to set which client system to use based on the incoming request http headers.
2.3 - The app.js file
In the main app.js file I again use app.use to use the middleware that I have defined in my set-client.js file.
|
|
I do so before anything else as it will not work if I set up my handler for the root path before hand, as the order in which things happen in express does very much matter.
2.4 - start up the demo
So now that I have everything in order I start up the demo by calling the main app.js file with node in the command line.
|
|
Once I have the message displayed in the command line I should be able to see what will happen when I navigate to localhost:8080 in my browser. If I do so in chrome I will get a different message compared to if I do so with some other browser as expected.
3 - Conclusion
The Express app use method is an important part of the app object in express. The method is needed to make use of express middleware that is made from the ground up for your own project, or added in via an additional module like with body-parser.