Express - app.router

In ExpressJS 3.x, you may see a line code:

1
app.use(app.router);

Briefly, app.router is the component that knows what to do when you call app.get() etc. See the Connect documentation for the router middleware to understand what it does.

Why is order important?

If the router middleware was installed before the session middleware, your router handlers would run before the sesssion middleware so the session wouldn’t be setup until after your handler completed; if the route middleware was installed after the errorHandler middleware, errorHandler wouldn’t get a chance to deal with error from your route handler because it would have been run before the router handler got called.

Bacically, you need to install app.router explicitly if you have other middleware that you need to ensure come after it in the stack.

References:

0%