繁体   English   中英

如何在Node.js中排队发布请求?

[英]How to queue the post request in Node.js?

当前的职位要求:

app.post('/rImage', rawBody, function (req, res) {
// ---- check here, if this process is running ? ----
  var data = req.rawBody;
  var escapedData = data.replace(/([<>|&^])/g, "\^$1"); // Windows CMD escaping
  exec('cscript /nologo C:/PS/Automation/render.vbs C:/PS/Automation/image.jsx"'+escapedData + '"',
    function (error, stdout, stderr)  {
      console.log('stdout: ' + escapedData);

      if(error !== null) {
        console.log('exec error: ' + error);
      }
      res.send(stdout);
      //request finished
    });
});

我如何为此实现一个队列系统,以便在最后一个请求完成时触发下一个请求?

提前感谢!

可能有图书馆。 但是,您可以这样修改它:

var queue = [];
var active = false;
var check = function() {
   if (!active && queue.length > 0) {
      var f = queue.shift();
      f();
   }
}

app.post("..", body, function(req, res) {
   queue.push(function() {
      active = true;

      exec("yourprogram arg1 arg2..", function() {
          // this is the async callback
          active = false;
          check();
      });
   });
   check();
});

这是一个显示功能的小提琴: https : //jsfiddle.net/fc15afw5/1/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM