Revisiting the router middleware
This chapter would be incomplete without revisiting the router
middleware.
The router
middleware is very special middleware. While other Express middlewares are inherited from Connect, router
is implemented by Express itself. This middleware is solely responsible for empowering Express with Sinatra-like routes.
Note
Connect-inherited middlewares are referred to in Express from the express
object (express.favicon()
, express.bodyParser()
, and so on). The router
middleware is referred to from the instance of the Express app (app.router
).
To refresh your memory, we learned in Chapter 2, Your First Express App, that if the router
middleware is not explicitly added in the middleware stack, it is added at the point where a route is defined for the first time. To ensure predictability and stability, we should explicitly add router
to the middleware stack:
app.use(app.router);
The router
middleware is a middleware system of its own. The route definitions form the middlewares...