检查申请状态并在可用时继续

Check application status and proceed when it avilible

我需要检查应用程序是否可用(应用程序是通过 child_process 节点 API 启动的),应用程序启动的时间可以更改为 2 - 30 秒。如果应用程序不可用,我需要使用 process.exit(1);

停止进程

我考虑检查应用程序端口是否打开
https://github.com/baalexander/node-portscanner

portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
  // Status is 'open' if currently in use or 'closed' if available
  console.log(status)
})

假设我有应用程序端口(作为参数)我应该怎么做 很好(超时?)每 100 毫秒?

看看 Javascript 计时事件:http://www.w3schools.com/js/js_timing.asp

您可以使用 setInterval(function, milliseconds) 每 100 毫秒执行一次端口扫描,但要注意:间隔越短,越接近危险区域,因为端口扫描会消耗扫描机器和扫描机.

请参阅下面来自 W3S 的示例:

<!DOCTYPE html>
<html>
<body>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
<script>
  var myVar = setInterval(myTimer, 1000);

  function myTimer() {
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toLocaleTimeString();
  }
</script>
</body>
</html>

在你的情况下它可能是(如果 checkPortStatus 是你想要的;)):

var myTimer = setInterval(scan, 100);
function scan() {
   portscanner.checkPortStatus(3000, '127.0.0.1', function(error, status) {
     // Status is 'open' if currently in use or 'closed' if available
     console.log(status)
     if (status == 'closed') {
       clearInterval(myTimer);
       doYourThing();
     }
   });
  }