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

Restify res.send () not called inside event listener callback

我正在使用 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 ()。一样。未触发。

非常感谢任何建议。

route.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 ();
  });

});

receiver.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

如果我用来自接收器的另一个 send/receive 消息队列替换事件发射器,则会调用 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));
});

你能试试吗res.json({'status':200}) ?

问题看起来像 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
});

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