从ChildProcess捕获EADDRINUSE

时间:2019-05-23 09:34:51

标签: node.js error-handling child-process httpserver

我有一个运行http服务器的生成进程。这是我的简化代码:

const params = ['node_modules/http-server/bin/http-server', '--cors', '-p'];
const proc = cp.spawn('node', params, { stdio: 'inherit' });
// the next callback is never triggered
proc.on('error', err => {
  log(err.toString(), LogType.Error); // this is my custom log method
  process.exit(1);
});

如果未正确清理先前的进程,则该端口仍在使用中,从而导致 EADDRINUSE 错误。我试图捕获该错误以将其正确报告给用户并退出该过程,但是我无法捕获该错误。

我尝试使用on,发出一次addListener,stderr,但没有任何错误可以捕获。

该过程仍然卡住,除了内部错误外,没有任何记录:

Error: listen EADDRINUSE: address already in use 0.0.0.0:8082
  at Server.setupListenHandle [as _listen2] (net.js:1259:14)
  at listenInCluster (net.js:1307:12)
  at doListen (net.js:1446:7)
  at processTicksAndRejections (internal/process/task_queues.js:81:17)
  at process.runNextTicks [as _tickCallback] (internal/process/task_queues.js:56:3)
  at Function.Module.runMain (internal/modules/cjs/loader.js:880:11)
  at internal/main/run_main_module.js:21:11

Emitted 'error' event at:
  at emitErrorNT (net.js:1286:8)
  at processTicksAndRejections (internal/process/task_queues.js:81:17)
  [... lines matching original stack trace ...]
  at internal/main/run_main_module.js:21:11

0 个答案:

没有答案