如何在Express 4.x中链接路由控制器
How do I chain route controllers together in Express 4.x?
所以,我使用express。我想使用next()将控制器连接在一起。
我有三个。他们做不同的事情,我想把他们混搭在一起。如果checkEmailVerification是坏的,则进入失败页面,如果是好的则进入next()。其他两个紧随其后()。completeE()将把它全部包起来。
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails,
app.core.users.completeEmailVerification
我的第一个想法是将其表达为:
// Confirm users email
app.route('/auth/confirm/:confirmationCode').get(
function(req, res, next) {
app.core.stack(req, res, next, [
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails
]);
}
);
所以,我写了下面的函数作为对我自己的编程挑战。
它不工作。所以我的两个问题是:
最好的方法是什么?可能已经有专门的东西了。编辑:也许这是它。//确认用户邮件app.route('/身份验证/确认/:confirmationCode ') . get (app.core.users.checkEmailVerification);app.route('/身份验证/确认/:confirmationCode ') . get (app.core.users.processVerification);app.route('/身份验证/确认/:confirmationCode ') . get (app.mean.subscribeMarketingEmails);
你能看到我在代码中错过了什么吗?递归没有更新'current'。输出如下:
谢谢!
// FIXME: this needs work
/**
* Stack req controllers through the use of next()
*
* E.g. app.core.stack(req, res, [
first,
second,
third
])
*
* == more or less ==>
*
* first(req, res, second(req, res, third(req, res)))
*/
core.stack = function(req, res, next, controllers, current) {
// current defaults to 0;
current = current || 0;
console.log('--> Current in stack:'+current);
// Check if something went wrong.
if (current < 0 || current >= controllers.length)
throw "core.stack() parameter current is invalid";
// If this is the last controller. Complete.
if (current == controllers.length-1)
return controllers[current](req, res, next);
// We're in the middle somewhere. Wrap in core.stack().
current++;
function wrap() {
console.log('--> Current in wrapper:'+current);
return core.stack(req, res, controllers, current)
}
return controllers[current](req, res, wrap);
}
输出:--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn''t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
GET /auth/confirm/adsfasdfasdf 500 61ms
--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn''t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
--> Current in stack:0
TypeError: Property '1' of object function (req, res, next) {
if (true) {
console.log("checking. Success!");
// process the next action in the route
next();
} else {
// on failure, flash a message.
console.log('Huh. That didn''t work. Login and click the resend verification email link');
}
},, is not a function
at Object.core.stack (/home/michael/scm/mean-core/core.js:47:32)
at Object.handle (/home/michael/scm/writermustwrite.com/app/routes/users.server.routes.js:24:13)
at next_layer (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/route.js:107:5)
at c (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:195:24)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:270:14)
at param (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:282:16)
at Function.proto.process_params (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:298:3)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:189:19)
at next (/home/michael/scm/writermustwrite.com/node_modules/express/lib/router/index.js:166:38)
对于这个错误,您可以调试(https://github.com/node-inspector/node-inspector),看看控制器函数内部发生了什么。
对于逻辑,我认为你错过了第一个控制器调用,因为current++在控制器[current]之前,所以你永远不会调用第一个控制器。
对于解决方案,您可以为同一路径添加多个路由处理程序。next
变量指向同一路由的下一个处理程序。因此,您可以使用更简化的代码实现相同的功能。
app.route('/auth/confirm/:confirmationCode')
.get(app.core.users.checkEmailVerification)
.get(app.core.users.processVerification)
.get(app.mean.subscribeMarketingEmails);
或者
app.route('/auth/confirm/:confirmationCode').get(
app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails);
控制器函数将被传递req, res和next对象
- 在 Apache 中使用 ngRoute 进行角度路由深度链接
- GET 404 (未找到) - 角度 js 链接路由
- 如何将活动类分配给与号.js中的当前路由链接
- 骨干、路由、链接
- 允许带有哈希的 href 链接绕过 SammyJS 路由
- 将指向域基的链接重新路由到指定目录
- 单击链接时,我如何定义导航到详细信息页面的路由
- ng路由以读取链接并定位具有相同名称的特定控制器
- Hashtags,如何用链接/路由替换字符串中的文本
- 停止Angular在ng-view中路由链接
- Angular JS路由——SPA内部的链接
- 为什么Jade认为来自我数组的链接应该将用户路由到/admin/thisurl而不是实际链接
- 如何链接JS路由变量和Symfony 2 url
- 当呈现的操作链接作为查询字符串返回时,我如何修复MVC3的路由问题
- 如何在Express 4.x中链接路由控制器
- 当直接链接时,AngularJS路由不工作——使用html5模式和Firebase重写
- react router链接高亮显示同级路由处于活动状态
- 由jQuery插件生成的Aurelia路由链接
- 我可以'我没有成功地链接我的路由处理程序
- 带页面的聚合物路由.js在新选项卡中打开链接时,不要向 URL 添加哈希标记