node.js - process.on ('SIGINT' 多个终止信号

标签 node.js

我有这个 Node 代码检测何时按下 Ctrl+C 以在 Node 应用程序退出之前做一些事情

process.on('SIGINT', function() {
    /* DO SOME STUFF HERE */

    process.exit()
})

现在这可行,但我想向这个过程添加其他终止信号,以便当 Ctrl+C、Node 应用程序退出或服务器重新启动或关闭或任何其他原因 Node 应用程序退出以触发终止信号并调用此过程和在退出之前在数据库中做一些事情...

我需要添加的终止信号是:

SIGTERM
SIGINT
SIGQUIT
SIGKILL

所以我想到是否可以这样做:

 process.on('SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGKILL', function() {
        /* DO SOME STUFF HERE */

        process.exit()
 })

所以要将多个终止信号传递给处理函数..我在上面添加了这样的内容,但它不起作用...这在 Node js 中如何完成?

最佳答案

最简单的方法:

['SIGINT', 'SIGTERM', 'SIGQUIT']
  .forEach(signal => process.on(signal, () => {
    /** do your logic */
    process.exit();
  }));

关于node.js - process.on ('SIGINT' 多个终止信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908853/

相关文章:

javascript - 将响应消息从一个客户端发送到另一个 socket.io 时出现问题

node.js - Express/Multer 在 ucs2 时检测文本文件的 7 位编码。它们是同义词吗?

node.js - Node.js 中 SERIAL 的外键违规

javascript - 如何使用变量args调用类方法

javascript - Lodash映射并返回唯一

javascript - 具体网址路径

node.js - 如何使用 Node js中的方法获取所有js文件操作

javascript - Node :-How to store image from a form in mongo DB using multer and send the same image to mail as an attachment

node.js - NetBeans 上的 WebApp 项目显示 : "Node.js: Missing node.js sources (in NodeJsWebApplication)"

node.js - 在服务器端使用 node.js + express.js 在 SPA(单页应用程序)中动态生成内容的 SEO