快速路由器绕过
在快速服务器中,我正在使用用户根据权限显示不同的内容。
app.use("/user", ensureAdmin, ...myAdminFunc); app.use("/user", ...myFunc);
现在,在我真正的代码中,我把它分解成了它自己的Express.Router对象。 并做了app.use(adminRoutes);
目前所有简单的东西。 我发现next("route")
函数不能按照我预期的方式工作。 我已经读了,我仍然需要在路由器内的每个路由上使用ensureAdmin
中间件function。
我现在有下面。
function ensureAdmin(req, res, next) { /*if not admin*/ next("route"); } admin.get("/user", ensureAdmin, ...myAdminFunc); admin.get("/other", ensureAdmin, ...myOtherAdminFunc);
我希望我能做一个中间件,并绕过整个路由器,如果需要的话。 诸如此类的东西
function ensureAdmin(req, res, next) { /*if not admin*/ next("skip everything in this router"); } admin = new Express.Router(); admin.use(ensureAdmin); admin.get("/user", ...myAdminFunc); admin.get("/other", ...myOtherAdminFunc);
但是当我在ensureAdmin中调用next("route")
,它仍然继续到admin.get("/user", ...myAdminFunc);
部分。
我已经读过,这是 next("route")
的预期行为
但想知道是否有一种方法,我还没有能够find它尚未绕过这个pipe理路由器集合中的一切? 也许next("router")
什么的?
我知道路由器本身不算作一个“路由”,而是一个路由集合,现在为什么next("route")
不起作用。
- 科林