how router object of express.js is parsed to app.use() which accept callbacks only as router is an object of express , I want to understand why app.use() is not throwing an error even if router is a object and app.use() demands callbacks?
From the app.use
documentation:
callback
Callback functions; can be:
- A middleware function.
- A series of middleware functions (separated by commas).
- An array of middleware functions.
A combination of all of the above.
...
Since
router
andapp
implement the middleware interface, you can use them as you would any other middleware function.
(my emphasis at the end)
So that's the answer. Both app
and router
are functions, and they follow the middleware protocol when you call them. (Remember that functions in JavaScript are objects, so they can have properties and methods on them.)