nodejs:在http.createServer关闭时释放资源

时间:2019-06-18 18:50:38

标签: node.js resources shutdown application-shutdown

我使用节点开发了代理服务器。我用这样的东西启动代理(也就是说,我没有使用express,只是普通的http模块):

:::

在中间,我启用了一个sql服务器连接池,并且我想确保在服务器关闭时(有意或由于某种异常)关闭该池并清除所有内容。 / p>

哪种方法可以确保关闭服务器或杀死节点进程时执行某些清理功能?

1 个答案:

答案 0 :(得分:1)

您可以在可能导致您被终止的事件集上设置侦听器-exitSIGINTSIGTERMuncaughtExceptionunhandledRejection

process.on('exit', (code) => {
  console.log(`About to exit with code: ${code}`);
  // Your cleanup code here
});