Express.js app.all method for handing all incoming requests
So one of the application methods in express.js is app.all, which is a method that can be used to work with any kind of http request method. The most commonly used methods are of course ‘GET’, and ‘POST’. However there are many more that also make sense for what they are, and at times it might be desirable to have a way work with any kind of incoming request regardless of the certain method. This is where app.all can be of help. In this post I will be writing about the app.all method in express, I will be showing some use case examples, and will touch base on the different http methods.
1 - Express app all method and what to know before starting
This is a post on the app.all method in express.js, it is not a getting started post on express.js or any additional subjects that relate to express.js. If you are new to express, you might wan to start with my getting started post on express.js, or my main post on express. Also in this post I am using express 4.16.3, and yes express is something where the version number matters a lot.
2 - Example of app.all in express
So to set this up I made my demo folder, made it the current working directory, and did a npm init to set up the package.json file. Once that was done the only package that I will be installing via npm is express itself, using the –save flag to add it to package.json.
I also added a public folder, and a js folder inside of it. This will house some simple static assets that I will used to make requests.
2.1 - The /public folder
2.1.1 - The /public/index.html file
For now this index.js file does not do much of anything aside from loading, and running these two scripts. In a more advanced version I have this do a bit more, but for now this is just the basic demo here.
2.1.2 - Using axios for a http client at /public/js/axios.min.js
So for this demo I just grab a copy of axios at the official repository dist folder found at github at https://github.com/axios/axios/blob/master/dist/axios.min.js. I just copied and pasted it in there to keeps things simple when it comes to including front end code in a simple demo like this.
2.1.3 - The /public/js/client.js file
So for this demo I have a client.js file in which I will be making some additional requests aside from the GET requests for the static assets.
For now I am just making a GET request for a path that does not exist, in a more advanced demo I could do some other kinds og requests other than GET requests.
2.2 - The /app.js file
So in the root of the demo folder is where I typically place my main app.js file that will be called with node to start the demo. I am using app.all to define middleware for all types of requests, coming from all possible paths by using an asterisk for the path.
In here I am using app.all to log to the console, any kind of incoming request. For this basic example so far it will only be GET requests, but app.all differs from other methods like app.get, or app.post in that it will apply to any kind of http method.
I also set the path to ‘*‘ this will make it so the method I give to app.all will not just respond to any kind of request, but also at any path. This allows for a sure ‘catch all’ method of sorts in which I want to do something for any kind of request made to any kind of path. So it goes without saying that app.all can come in handy with many kinds of scenarios.
Notice that I have one such method at both the beginning, and end of the file. When adding one at the very beginning it allows for me to do something that should be done first before proceeding with anything else. In this simple demo I am just logging what is going on to the console, but it could be some kind of request sanitation check when making a real project. In addition I also have one at the very end, which can be used to respond to what should typically be 404 requests, but also any other kind of request that was not satisfied by any anything that is going on above.
3 - Conclusion
So the app.all method is the most versatile express.js app method of the app methods that have to do with handing incoming http requests. There are many other methods such as app.get, and app.post that can also be used, but they are reserved to just requests of a certain http method. It does make sense to use those of course as a way of breaking code up more, but if for some reason I do want to write something that will apply to all requests there is of course app.all for that.
Thank you for reading, if you enjoyed this post you might want to check out my other posts on express.js.