Express - a middleware engine
Middleware are functions that have access to the request and response object, they are involved on pre-processing both the objects before it reaches the final destination. I like to think in express and middlewares like factory treadmill, where req. and res. are a box the pass through machines that can modify the product along the way, then deliver.
When you call a function inside another function, javascript does put this function on a structure called “stack“ to be resolved. The stack works like a pile of clothes, if a function is called inside another function, is placed on top of the stack to be resolved first, and like in a pile of clothes, to reach the first one in the pile you need to get all above before.
Thats how middleware works, they are added to the stack to process the content. Middleware 3 will be processed, then middleware 2, middleware 1, and then the “official handler”. Technically official handler it’s also a middleware, just happens to be the last one to process the information before reponse is send
In the body of a middleware that is another function called Next( ), that passes the control to the next middleware on the stack. So if my “offical handler” it’s responsible to send out the request, and “middleware 1” does not call “next( )” the request will be left hanging and the response will not be sent.
Defining middlewares and routes
It was hard for me to understand from documentation, but there are 2 different types of middlewares : “globals” and “per route”, at least that’s how I call them..
Global middlewares
To add global middlewares, middlewares that will be processed in each and any response to the server, it is just needed to pass the middleware as argument to app.use( ), this will make the middleware avaiable to all incoming requests, regardless of the route.
Per route middlewares
app.use( ) in reality takes two arguments: app.use (path, callback). When path it’s not passed, the middleware is applied globally. To make an middleware avaiable just through one route, the path of the rout must be passed as first argument, and them the callback. app.use (‘/homepage’, middleware).
Since middleware works on a stack, the order of declaration will be relevant in the application logic.
app.route( )
app.route( ) is a method provided by Express.js that creates a new route and chain route handlers for the specified path. It allows us to group route handlers for a particular path together, making our code more organized, consized and easier to read.
app.route( ) its just a fancy way to organize middlewares on the same route, chaining and encapsulating all methods in a specific path to later attach it to the main application.
modulating routes and middlewares
express.route( ) is a class in the expressthat allows us to create modular and mountable route handlers. It works by creating a new instance of the Router
class and then defining routes on that instance in another file, to further export them.
on another file, express.router( ) is called instead of app. At this point the middlewares mechanisms will work on the same way. The only difference is that when importing to the main file, the code will be more organized
Now that the users routes are created and imported, it’s just a matter of mont the routes on top of ‘/users’ path in the app.use( ). the router.route(‘/’) path will correspond to ‘users/’, ‘/:id’ will correspond to ‘/users/:id’ and so on, it will works like an autocomplete, where the router.route( ) routes will always be completed by the /users path that comes before.
“/’ becomes ‘users/’
‘/:id becomes ‘users/:id’.