What’s the difference between application- and router level- middleware when routing in Express?

What is the purpose of app.use passing it to router.get instead of simply using app.get?

This is all just designing for modularity. It allows an application to be divided into several smaller loosely-coupled pieces and because they all do not have any direct knowledge of the shared app instance that combines them together, the modularity is achieved.

For example, you could build a whole user account subsystem that handles sign up, login, forgot password, etc, and share it between several different applications by “mounting” it within them via app.use(require("my-user-system")).

That’s the only purpose behind this. There no functional, technical, or performance difference otherwise.

And generally, what’s the difference between app.VERB and router.VERB in terms of routing?

No difference. The app has it’s own main/primary router and app.VERB is just convenience sugar for the equivalent of app.router.VERB.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)