Express 进阶:从一个例子看路由分组机制
路由是 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 routerif (req.params.id === '0') next('route')
// otherwise pass control to the next middleware function in this stackelse 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');
};
})
相信有同样疑惑的同学不少,因为官网对 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的路由比较复杂,篇幅所限,这里不继续展开,感兴趣的同学可以留言交流。如有错漏,敬请指出。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论