Detail of express and koa, especially the theory of midware and the difference between them.
对于一个express应用,本质上就是在调用各种中间件。中间件功能包括:
- 执行任意代码
- 修改请求对象(req)和响应(res)对象
- 结束请求并作出响应
- 调用下一个中间件(next)
app.use
, 注意:不挂载路径所有请求都会经过该中间件app.METHOD
, 如app.get
,app.post
等指定处理特定http method的中间件
next()
表示将控制权交给栈中下一个中间件next('route')
表示跳到下一个路由
next('route')
只对使用 router.VERB()
或 app.VERB()
加载的中间件有效, 对app.use
和router.use
无效
Router -> Route -> Layers -> handles
- 一个Router包含多个Route
- 一个Route包含多个Layer(每个layer都将route包装到layer.route),一个Route对应一个路由
- 一个Layer对应一个路由中间件函数
Route.prototype.dispatch
中通过next获取存放在栈中的每一个layer来执行中间件(顺序执行)
Router内部遍历栈Layer, Layer取对应route,然后再经历上一步骤