koa-router源码地址是 koa-router
当前解读版本为7.2.1javascript
代码结构图
执行流程图
关系对应图
html
注册注册路由的方法,结果就是Router的原型上面多了get,post,delete,del等注册路由的方法
代码使用的时候 r1.get就是这么来的java
r1.get('/test1/:id', function (ctx, next) { console.log('test1 :1') next() }, function (ctx, next) { console.log('test1:2') })
等同于 Router.prototype.deletenode
注册中间件,支持形式多种多样
看着这么多,其实就两种中间件,git
重点就是router.routes()返回的这种件,须要的前缀,参数验中间件作一些处理github
router.use(function (ctx, next) { ctx.foo = 'baz'; return next(); }); router.use('/foo/bar', function (ctx, next) { ctx.foo = 'foo'; return next(); }); router.use('/foo', subrouter.routes()); router.use(['/foo', '/bar'], function (ctx, next) { ctx.foo = 'foo'; ctx.bar = 'bar'; return next(); }); parentRouter.use('/parent-route', function (ctx, next) { ctx.n = ctx.n ? (ctx.n + 1) : 1; return next(); }, nestedRouter.routes());
给router实例添加前缀,前缀能够是包含参数的segmentfault
router.prefix('/things/:thing_id')
返回中间件,
中间执行的时候,会根据path获取知足匹配条件的路由(Layer),而后根据每一个Layer生成一个解析参数值的中间,这就是为何咱们在ctx.params能获得参数值
最核心的代码以下数组
layerChain = matchedLayers.reduce(function(memo, layer) { memo.push(function(ctx, next) { ctx.captures = layer.captures(path, ctx.captures); ctx.params = layer.params(path, ctx.captures, ctx.params); return next(); }); return memo.concat(layer.stack); }, []); return compose(layerChain)(ctx, next);
matchedLayers是匹配的Layer或者说一条路由信息,同一个路径一样的方法也是会生成两条记录的,以下一样的注册,会生成两个不一样路由(Layer),哪怕信息如出一辙app
r1.get('/test1', function (ctx, next) { console.log('test1 :1') next() }) r1.get('/test1', function (ctx, next) { console.log('test1 :2') next() })
matchedLayers.reduce没执行一次,是生成两个中间件,
一个是参数解析的中间件,这就是为何你能够经过ctx.params取值到路由参数了koa
function(ctx, next) { ctx.captures = layer.captures(path, ctx.captures); ctx.params = layer.params(path, ctx.captures, ctx.params); return next(); }
另一个才是实际路由匹配的执行方法,上面的demo就是
function (ctx, next) { console.log('test1 :1') next() }
此方法执行很靠后,在他后面注册的中间件执行完毕后才执行
生成一个中间件,做用是定义路由没匹配到,方法未容许,方法未实现等的返回信息
app.use(router.routes()); app.use(router.allowedMethods({ throw: true, notImplemented: () => new Boom.notImplemented(), methodNotAllowed: () => new Boom.methodNotAllowed() }));
注册一个路由,容许全部的get,post等方法访问
跳转,原理就是注册了一个路由,用ctx.redirect来实现跳转
router.redirect('/login', 'sign-in');
等同于
router.all('/login', function (ctx) { ctx.redirect('/sign-in'); ctx.status = 301; });
核心方法之一,注册中间件
Router.prototype.all,methods.forEach等底层都是调用这个家伙实现的
Router.prototype.use也依据状况会调用
查找具名的route(Layer)
生成url,能够传参
router.get('user', '/users/:id', function (ctx, next) { // ... }); router.url('user', 3); // => "/users/3" router.url('user', { id: 3 }); // => "/users/3" router.use(function (ctx, next) { // redirect to named route ctx.redirect(ctx.router.url('sign-in')); })
得到匹配的路由(Layer),以path和method来过滤的
router.routes返回的中间件底层就是经过他来确认请求应该进入哪些路由的
添加参数验证中间件,这个须要结合Layer.prototype.param 一块儿来理解
Router.prototype.param = function (param, middleware) { this.params[param] = middleware; this.stack.forEach(function (route) { route.param(param, middleware); }); return this; }; Layer.prototype.param = function (param, fn) { var stack = this.stack; var params = this.paramNames; // 构建参数验证中间件 var middleware = function (ctx, next) { return fn.call(this, ctx.params[param], ctx, next); }; middleware.param = param; var names = params.map(function (p) { return p.name; }); var x = names.indexOf(param); if (x > -1) { // iterate through the stack, to figure out where to place the handler fn stack.some(function (fn, i) { // param handlers are always first, so when we find an fn w/o a param property, stop here // if the param handler at this part of the stack comes after the one we are adding, stop here // fn.param 做为判断是否是参数验证中间件的标志 // 若是不是参数验证中间件,或者参数验证中间件须要验证的参数在我以后,插入参数验证中间件 // 好比说path是这样的 /user/:id/posts/:postid, 那么id参数验证中间件应该在postid参数以前 // 简单说,确保参数按照顺序被验证 if (!fn.param || names.indexOf(fn.param) > x) { // inject this param handler right before the current item stack.splice(i, 0, middleware); return true; // then break the loop } }); } return this; };
path是否匹配路由
得到路由参数键值对
得到路由参数的值
用参数构建URL,params参数可视是对象也但是数组
添加参数验证中间件
设置前缀
decodeURIComponent错误时返回原值
更多细节请直接看带备注的源码吧,写东西真累啊!
Router
Layer
koa-router 源码解析 - segmentfault
解析Koa-Router,迈入Web次时代第一步(上)
Koa-Router 源码解析下 -CNode
koa-router源码解读