图解 Express 中间件结构

431 阅读1分钟
methods.forEach(function(method){
  app[method] = function(path){
    if (method === 'get' && arguments.length === 1) {
      // app.get(setting)
      return this.set(path);
    }

	// 懒加载路由匹配器
    this.lazyrouter();

    var route = this._router.route(path);
    // app 代理了 _route 的操作
    route[method].apply(route, slice.call(arguments, 1));
    return this;
  };
});

app 对中间件的操作就是 _router 对中间件的操作