如何在Express 4.x中将路由控制器链接在一起? [英] How do I chain route controllers together in Express 4.x?

查看:61
本文介绍了如何在Express 4.x中将路由控制器链接在一起?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在使用快递.我想使用next()将控制器链接在一起.

So, I'm using express. I'd like to chain controllers together using next().

我有三个.他们做不同的事情,我想将它们混合在一起.如果不好,checkEmailVerification转到失败页面,如果不好,则进入next().另外两个仅是next(). completeE()会将其全部包装.

I've got three. They do different things and I'd like to mix and match them together. checkEmailVerification goes to a fail page if bad, or next() if good. The other two just next(). completeE() would wrap it all up.

app.core.users.checkEmailVerification,
app.core.users.processVerification,
app.mean.subscribeMarketingEmails,
app.core.users.completeEmailVerification

我首先想到的是表达为:

My first thought was to express this as:

// 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
        ]);
    }
);

因此,我将下面的功能写成了自己的编程挑战.

So, I wrote the function below as a programming challenge for myself.

它不起作用. Cestest vi.所以我的两个问题是:

It doesn't work. C'est la vi. So my two questions are:

  1. 执行此操作的最佳方法是什么?可能已经为此做了一些事情. 也许就是这样. //确认用户的电子邮件 app.route('/auth/confirm/:confirmationCode').get(app.core.users.checkEmailVerification); app.route('/auth/confirm/:confirmationCode').get(app.core.users.processVerification); app.route('/auth/confirm/:confirmationCode').get(app.mean.subscribeMarketingEmails);

  1. What's the best way to do this? There is probably something already made for this. Maybe this is it. // Confirm users email app.route('/auth/confirm/:confirmationCode').get(app.core.users.checkEmailVerification); app.route('/auth/confirm/:confirmationCode').get(app.core.users.processVerification); app.route('/auth/confirm/:confirmationCode').get(app.mean.subscribeMarketingEmails);

您可以看到我在代码中遗漏的内容吗?递归不会更新当前".输出如下:

Can you see what I missed in the code? The recursion isn't updating 'current'. The output is below:

谢谢!

  // 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)

推荐答案

对于错误,您可以调试(

For the error, you could debug (https://github.com/node-inspector/node-inspector) and see what's happening inside the controller functions.

从逻辑上讲,我认为您错过了第一个控制器调用,因为current ++在controllers [current]之前,因此您永远不会调用第一个控制器.

For the logic, i think you are missing the first controller invocation as current++ is before controllers[current] and so you would never invoke the first controller.

对于该解决方案,您可以为同一路径添加多个路由处理程序. next变量指向同一路由的下一个处理程序.因此,您将可以使用简化得多的代码来实现相同的目的.

For the solution, you could add multiple route handlers for the same path. next variable points to the next handler for the same route. so, you would achieve the same thing with much simplified code.

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和下一个对象.

the controller functions will be passed req, res and next objects.

这篇关于如何在Express 4.x中将路由控制器链接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆