gpt4 book ai didi

node.js - 无法调试node.js "stop"事件并处理 "exit"事件

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:22 24 4
gpt4 key购买 nike

我正在尝试调试 Node JS 服务器的退出/停止事件,并且我阅读了以下主题:

Close event

Process events

尝试实现它们,但当我单击 WebStorm IDE 中的“停止”按钮时,我似乎无法在断点处停止。

我的 bin/www 文件中有以下代码:

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
process.stdin.resume();//so the program will not close instantly

function exitHandler(options, err) {
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}

//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));

//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));

//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));

但是当我停止调试 session 时,上述内容都不会被触发。

我也尝试过使用 server.on('stop',...) 但没有成功。

我怀疑按 IDE 中的 stop 按钮来停止调试 session 并不是调试这种情况的正确方法,但由于我是 Node.js 的新手,也许我的代码有问题。

**编辑:**我还尝试了 SIGTERMSIGKILL 事件。我使用的是 iOS。

最佳答案

我今天使用与您相同的代码遇到了同样的问题。这与 Node.js 处理这些事件的方式有关。调试器服务不再通过主事件循环提供服务,因此调试器无法发出自己的事件来让 ide 知道“中断”(因此没有 ide 交互)。

关于node.js - 无法调试node.js "stop"事件并处理 "exit"事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37119483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com