Doing a cleanup action just before Node.js exits

后端 未结 11 1555
时光说笑
时光说笑 2020-11-22 13:54

I want to tell Node.js to always do something just before it exits, for whatever reason — Ctrl+C, an exception, or any other reason.

I tried th

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 14:15

    This catches every exit event I can find that can be handled. Seems quite reliable and clean so far.

    [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach((eventType) => {
      process.on(eventType, cleanUpServer.bind(null, eventType));
    })
    

提交回复
热议问题