express.js 的路由器对象如何解析为仅接受回调的 app.use()?

how router object of express.js is parsed to app.use() which accept callbacks only?

如何将 express.js 的路由器对象解析为 app.use() 仅接受回调,因为路由器是 express 的对象,我想了解为什么 app.use() 不是即使路由器是一个对象并且 app.use() 需要回调也会抛出错误?

来自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 and app implement the middleware interface, you can use them as you would any other middleware function.

(我强调在最后)

这就是答案。 approuter都是函数,调用时遵循中间件协议。 (请记住,JavaScript 中的函数是对象,因此它们可以具有属性和方法。)