Restify.js(Connect / Express)中间件忽略对promise回调中的next()的调用

时间:2013-06-12 20:21:19

标签: node.js express connect q restify

因此。在尝试为Restify.js应用程序实现一些基本中间件时,我遇到了一些怪癖,我正在构建特定于next()并承诺回调。

以通用形式表达问题:

  var server = restify.createServer({
    name: config.name
  });

承诺解决:

  server.use(function checkAcl(req, res, next) {
    // Promise is resolved
    var promise = function () {
      var deferred = require('q').defer();
      deferred.resolve();
      return deferred.promise;
    }

    promise()
      .then(function () {
        next(); // doesn't get 'called', no response sent, connection eventually times out
      }, function () {
        res.send(new restify.NotAuthorizedError());
      });
  });
  server.use(restify.bodyParser());

  ...

承诺被拒绝

  server.use(function checkAcl(req, res, next) {
    // Promise is rejected
    var promise = function () {
      var deferred = require('q').defer();
      deferred.reject();
      return deferred.promise;
    }

    promise()
      .then(function () {
        next(); 
      }, function () {
        res.send(new restify.NotAuthorizedError()); // this works fine
      });
    }
  });

  server.use(restify.bodyParser());      

  ...

我做了什么明显错误的事吗?任何见解?它似乎与承诺回调相关,它们是否以某种方式抑制了对next()的调用?

1 个答案:

答案 0 :(得分:0)

在自定义中间件解决问题之前添加restify.bodyParser()中间件。

n.b:

  // Add bodyParser() to the stack before the custom middleware
  server.use(restify.bodyParser()); 

  server.use(function checkAcl(req, res, next) {
    // Promise is resolved
    var promise = function () {
      var deferred = require('q').defer();
      deferred.resolve();
      return deferred.promise;
    }

    promise()
      .then(function () {
        next(); // next() now behaves as expected.
      }, function () {
        res.send(new restify.NotAuthorizedError());
      });
  });
  ...

除此之外,如果你的中间件的顺序很重要(它可能会这样做),我最终还是this Github issue描述了这个问题以及另一个可能的解决方案。

  

mcavage:非常确定这实际上是节点会咬你(就像你的数据被发射到无处)。试试这个:

  var server.createServer();
  server.pre(restify.pre.pause());
  ...