javascript - Restify res.send () 未在事件监听器回调内部调用

标签 javascript node.js express mq restify

我正在使用 Restify 2.8.4、nodejs 0.10.36 和 IBM MQ Light 消息传递。

这是一种 RPC 模式,每当接收者准备好结果时,它就会发出一个事件,下面的 Restify POST 路由将捕获事件和数据,并用 res.send 回复().

问题是,res.send () 未触发。我不明白为什么 res.send () 没有被触发。此 POST 路由中只有一个 res.send ()

尝试了res.write()res.end()。一样。未触发。

非常感谢任何建议。

路由.js

server.post ('/user', function (req, res, next) {
  var body = req.body;

  var recvClient = createRecv ();

  var sendClient = createSend (body);

  recvClient.on ("receiver:got:it", function () {
    // receiver has confirmed received message
    // so sender client can be released 
    console.log ('stopping send client...'.info);
    sendClient.stop ();
  });

  recvClient.on ("receiver:replied", function (data) {
    // event callback was triggered
    console.log ('receiver replied captured...');

    res.send ();
    return next ();
  });

});

接收者.js

recvClient.on('started', function() {
    recvClient.subscribe(topicPattern, {qos: 1, autoConfirm: false, credit: 1});

    recvClient.on('message', function(data, delivery) {
      console.log('Recv: ' + util.inspect (data, false, null));

      delivery.message.confirmDelivery (function () {
        recvClient.emit ('receiver:got:it'); 
      });

      var user = new User (data);     
      user.create ()           
      .then (function (user) { 
        recvClient.emit ('receiver:replied', user);
    }, function (err) {      
        recvClient.emit ('receiver:replied', {err: err.message});
    })
      .finally (function (){   
        console.log ('stopping receiver client...');
        recvClient.stop ();    
      });

    });
  });

输出跟踪

body: {}  //stuff here
Recv: {}  // receiver received body stuff
stopping send client...
// do work

receiver replied captured...
stopping receiver client...

更新1

如果我用接收器中的另一个发送/接收消息队列替换事件发射器,则会调用 res.send ()

这种方法比事件发射器更困惑。我仍然拒绝相信为什么事件发射器不起作用。

var rpcReceive = new RpcReceive ("user.create.replied", {qos:1, autoConfirm: false, credit: 1});

rpcReceive.receive (recvClient)
.then (function (user){
  res.send (user);
  return next ();
})
.then (undefined, function (err){
  return next (new restify.errors.BadRequestError (err.message));
});

最佳答案

问题看起来像是在route.js的结构中。实际上,recvClient.on事件不应该依赖于server.post('/user')。正确的结构应该是这样的:

var recvClient = createRecv ();     // should be global object

server.post ('/user', function (req, res, next) {
  var body = req.body;

  //var recvClient = createRecv ();     //commented out this code as this is moved out of this API.

  //var sendClient = createSend (body); //commented out this code. May be this wont be required.

});

recvClient.on ("receiver:got:it", function () {
    // receiver has confirmed received message
    // so sender client can be released 
    console.log ('stopping send client...'.info);
    //sendClient.stop ();       //commented out this code. May be this wont be required.
});

recvClient.on ("receiver:replied", function (data) {
    // event callback was triggered
    console.log ('receiver replied captured...');

    //res.send ();              
    //return next ();       

    /*commented out these two lines. Instead of sending response like this, emit some event from here, which can be consumed on receiver.js. Use something like below code to send response to receiver.js */
    recvClient.emit('sending:response', "response");
});

您的receiver.js应该具有以下逻辑(除了您的代码之外)来接收来自route.js的响应。

recvClient.on('sending:response', function(response) {
    // do processing on response
});

请尝试此操作,如果发现问题请回复。我在创建聊天应用程序时研究了类似的逻辑来发出和消耗事件。如果您仍然遇到问题,请告诉我。

关于javascript - Restify res.send () 未在事件监听器回调内部调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31834934/

相关文章:

node.js - 错误! Windows_NT 6.3.9600,windows8无法安装cordova

javascript - 为什么 Node.JS 中的 V8 比我的原生 C++ 插件更快?

javascript - React Native 后台服务器启动报错

node.js - 在 Node.js + Express 中记录错误的任何实际稳定方法?

javascript - 未捕获的类型错误无法读取未定义的属性 'value'

关联数组中的 Javascript 函数不起作用

javascript - 无论如何,在 Navbar 中保持 Bootstrap Dropdown 打开

javascript - Windows 小工具 : Why are settings deleted when my gadget is closed?

node.js - 在编写使用 supertest 的测试时访问 req.session 对象

javascript - 这个错误说明了什么?类型 'ParsedQs' 不可分配给类型 'string'