如何在Express 4.x中链接路由控制器

How do I chain route controllers together in Express 4.x?

本文关键字:链接 路由 控制器 Express      更新时间:2023-09-26

所以,我使用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
        ]);
    }
);

所以,我写了下面的函数作为对我自己的编程挑战。

它不工作。所以我的两个问题是:

  1. 最好的方法是什么?可能已经有专门的东西了。编辑:也许这是它。//确认用户邮件app.route('/身份验证/确认/:confirmationCode ') . get (app.core.users.checkEmailVerification);app.route('/身份验证/确认/:confirmationCode ') . get (app.core.users.processVerification);app.route('/身份验证/确认/:confirmationCode ') . get (app.mean.subscribeMarketingEmails);

  2. 你能看到我在代码中错过了什么吗?递归没有更新'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对象