广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Express框架Router Route Layer对象使用示例详解
  • 903
分享到

Express框架Router Route Layer对象使用示例详解

Express使用Router Route LayerExpress 对象 2023-03-24 17:03:57 903人浏览 薄情痞子
摘要

目录引言LayerRouteRouter方法统计两个 stack取出 stack 中 layer从 Router 到 layer 的路径Router.route 方法中的 dispa

引言

这使用倒叙的方式: 将 Layer 放在最前面讲解,然后是 Route 路由项目,最后是 Router 路由器。

Layer

Layer 是什么? Express 中最小的存储单元,存储的重要内容包括 handle 也就是 fn、path 等路径。fn 就是中间件处理函数。重要的是 route 中能匹配:

module.exports = Layer;
function Layer(path, options, fn) {
  if (!(this instanceof Layer)) {
    return new Layer(path, options, fn);
  }
  var opts = options || {};
  this.handle = fn;
  this.name = fn.name || '<anonymous>';
  this.params = undefined;
  this.path = undefined;
  this.regexp = pathRegexp(path, this.keys = [], opts);
  this.regexp.fast_star = path === '*'
  this.regexp.fast_slash = path === '/' && opts.end === false
}
Layer.prototype.handle_error = function handle_error(error, req, res, next) {}
Layer.prototype.handle_request = function handle(req, res, next) {}
Layer.prototype.match = function match(path) {} // 返回 boolean

看看哪些的内容实例化了 Layer 的构造函数。Layer 的最主要的作用就是,被路由匹配到,然后取出 handle 函数最后调用,消费 handle 函数。

  • Route

一个 Route 的栈 stack 中,可以存放多个 Layer。

Route.prototype.all = function all() {
    
    var layer = Layer('/', {}, handle);
    
}
Route.prototype[method] = function(){
   
   var layer = Layer('/', {}, handle);
   
}
  • Router 中
proto.route = function route(path) {
  // ***
  var layer = new Layer(path, {
    sensitive: this.caseSensitive,
    strict: this.strict,
    end: true
  }, route.dispatch.bind(route));
 // ***
 this.stack.push(layer);
};
proto.use = function use(fn) {
     var layer = new Layer(path, {
      sensitive: this.caseSensitive,
      strict: false,
      end: false
    }, fn);
    this.stack.push(layer);
}

在 Router 中的 route 和 use 函数,使用 Layer 构造函数实例化 layer, 然后将 layer 压到 stack 中保存卡里,方便以后匹配。

  • layer 的匹配方法
function matchLayer(layer, path) {
  try {
    return layer.match(path);
  } catch (err) {
    return err;
  }
}

从上面的代码中知道,layer 对象的 match 方法,根据路径进行匹配, match 返回 boolean. 在匹配的时候主要处理了两个属性:

this.params = undefined;
this.path = undefined;

接下来看 matchLayer 函数, matchLayer 调用在 Router.handle 函数的 next 函数中。

Route

module.exports = Route;
function Route(path) {
  this.path = path;
  this.stack = [];
  this.methods = {};
}
Route.prototype._handles_method = function _handles_method(method) {}
Route.prototype._options = function _options() {}
Route.prototype.dispatch = function dispatch(req, res, done) {}
Route.prototype.all = function all() {}
// 扩展 methods 包中的方法

Router

Router 就是 proto

var proto = module.exports = function(options) {}
proto.param = function param(name, fn) {}
proto.handle = function handle(req, res, out) {}
proto.process_params = function process_params(layer, called, req, res, done) {}
proto.use = function use(fn) {}
proto.route = function route(path) {}
// 扩展 methods + all 上所有的方法

注意: Router.handle 函数.

var stack = self.stack;
while (match !== true && idx < stack.length) {}

在 while 循环中,使用 idx 中取出 layer 和 path然后交给 matchLayer 函数, 得到匹配结果。如果调用的内容正常:

layer.handle_request(req, res, next) // 最终会得到中间件的处理函数

接下来盘点, Router/Route/Layer 的常用方法

方法统计

  • Router
Router 方法说明
Router param参数
Router handle处理函数
Router process_params处理参数
Router use中间件
Router route路由
Router [methods]/all各种方法
  • Route
Route 方法说明
Route _handles_method私有处理函数
Route _options私有选项
Route dispatch派发请求和响应
Route all各种方法
Route [methods]各种方法
  • Layer
Layer 方法说明
Layer handle_error处理错误
Layer handle_request处理请求
Layer match根据路径匹配路由并返回 boolean

看 Router 和 Route 有相同的方法: all/[methods]。使用 Router.route 的方法通过 path 方法关联。同时 咋 Router.route 中实例化 Layer ,然后将 layer 保存在 Router 的 stack 中。

两个 stack

从上面的分析中,知道了 Router 中有 stack,Route 中也有 stack, 在 stack 中添加内容(也就是 Layer)一般都是与路由和中间件相关。

  • Router 的 use 方法中,包含了实例化 Layer, 并存储在 Router 级别的 stack 中。
  • Router 的 route 中,实例化了 Layer, 并存储在 Router 级别的 stack 中。
  • Router 的 [methods]/all 方法中,调用了 route 方法,自然也存储了 stack

取出 stack 中 layer

取出 Layer 发生在 Route 的 dispatch 函数 的 next 函数中,此时需要调用 layer 中匹配到的参数。

从 Router 到 layer 的路径

  • Router 是被 express 单独的输出出去的。
  • Router 实例化之后,可以调用 use/[methods] 实例化 Layer 并保存 stack 中,当然也可调用 Router.route 方法。

Router.route 方法中的 dispatch

var layer = new Layer(path, {
    sensitive: this.caseSensitive,
    strict: this.strict,
    end: true
}, route.dispatch.bind(route));

route.dispatch 在此处 bind 绑定,此时作为 Layer 构造函数的第三个参数,保存为 handle, 最后会被拿出调用。此时就进入了 next 函数调用阶段。

next 函数

next 函数是 Express 中间件的基础,dispatch 函数从 当前的 stack 中拿出 layer 的实际情况调用 layer 不同的方法。

if (layer.method && layer.method !== method) {
  next(err)
}

当 layer 中的方法或者等于但当前的方法时,调用自己,此时 next 函数发生了递归。否则进入 handle 相关方法处理请求和处理错阶段,此时 next 方法发生了递归调用。

小结

  • Router -> Route -> Layer
  • Router -> use -> Layer
  • Router 和 Route 都有自己 stack 存储 layer
  • next 函数在 Route 的 dispatch 被执行,并在 layer 相关属性中发生 next 函数的递归,实现了 Express 的中间件。

以上就是Express框架Router Route Layer对象使用示例详解的详细内容,更多关于Express使用Router Route Layer的资料请关注编程网其它相关文章!

--结束END--

本文标题: Express框架Router Route Layer对象使用示例详解

本文链接: https://www.lsjlt.com/news/201211.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作