文章概览
路由是Express的核心功能。本文通过一个简单例子,介绍Express的路由分组机制,以及next('route')的正确使用方法。
背景:关于next()的问题
使用过Express的同学都知道,通过next()将代码执行权转移到下一个中间件(例子略)。
在官网有下面例子,出现了next('route')的调用:
// a middleware sub-stack that handles GET requests to the /user/:id path
router.get('/user/:id', function (req, res, next) {
// if the user ID is 0, skip to the next router
if (req.params.id === '0') next('route')
// otherwise pass control to the next middleware function in this stack
else next()
}, function (req, res, next) {
// render a regular page
res.render('regular')
})
// handler for the /user/:id path, which renders a special page
router.get('/user/:id', function (req, res, next) {
console.log(req.params.id)
res.render('special')
})
差别就在于多了个参数,它跟普通的的next()有什么区别呢?上面代码实现的效果,用如下代码实现不是更简单吗:
app.get('/user/:id', function (req, res, next) {
if (req.params.id == 0) {
res.render('special');
} else {
res.render('regular');
};
})
在SF上也有同学表达了同样的疑惑《对express 应用级中间件next('route') 方法实例的疑惑》。
相信有同样疑惑的同学不少,因为官网对next('route')用法的介绍有点简略。下文将从Express的路由分组机制来讲回答这个问题。
Express路由分组机制
Express的路由内部实现比较复杂,这里只挑跟题目有关的讲。
Express中,路由是以组的形式添加的。什么意思呢,可以看下面伪代码
app.get('/user/:id', fn1, fn2, fn3);
app.get('/user/:id', fn4, fn5, fn6);
在内部,Express把上面添加的路由,分成了两个组。继续看伪代码,可以看到,路由在内部被分成了两个组。
var stack = [
{path: '/user/:id', fns: [fn1, fn2, fn3], // 路由组1
{path: '/user/:id', fns: [fn4, fn5, fn5] // 路由组2
];
路由匹配就是个遍历的过程,略。
next('route')是干嘛的
答案:跳过当前路由分组中,剩余的handler(中间件)
如果没有next('route'),一路next()调用下去的话,调用顺序是这样的:
fn1 -> fn2 -> fn3 -> fn4 -> fn5 -> fn6
假设某些情况下,在执行了fn1后,想要跳过fn2、fn3,怎么办?(比如楼主举的例子)
答案就是在fn1里调用next('route')。
然后就变成了
fn1 -> fn4 -> fn5 -> fn6
写在后面
写到这里,相信大家对Express的路由分组机制,以及next('route')的用法都有了一定了解。Express的路由比较复杂,篇幅所限,这里不继续展开,感兴趣的同学可以留言交流。如有错漏,敬请指出。
Express、koa2略有小研究,最近刚撸了一遍源码。另外,常年分享周边科普文,欢迎关注 我的GitHub 程序猿小卡,或者star 《Nodejs学习笔记》
后续会继续分享Express或koa2周边相关的技术文章 :-)