debuggingnode.js上的正常closures
我在我的app.js中有以下代码:
process.stdin.resume();//so the program will not close instantly //do something when app is closing process.on('exit',exitHandler.bind(null,{exit:true,event: "exit"})); //catches uncaught exceptions process.on('uncaughtException',exitHandler.bind(null, {exit:false,event: "uncaughtException"}));
我读了所有有关阅读有关closuresnode.js服务器,但无法find任何有关如何debugging它。
问题在于,当您单击debugging控制台中的“停止”button时,必须终止/终止节点进程。
有没有人有一个想法,我怎样才能debugging我优雅的关机代码? 在closuresdebugging会话时,debugging模式不会在exitHandler
内的断点上停止。
我正在使用WebStorm,但是我读了也可能发生在其他IDE上。
谢谢
那么,没有魔法。 要获得'exit'
事件,你的代码最终必须打到process.exit()
代码,并得到'uncaughtException'
你需要引发未处理的exception。
也许有一种疯狂的方式来触发这个代码而不创build实际的事件,但是你应该坚持正常的程序生命周期,因为这就是事物在生产中的工作方式,而这绝对是你想要testing的。
练习:如果你想看到一次,如果你是正确的,你可以手动添加适当的代码只是你的应用程序,所以它会被触发一旦加载:
setTimeout(function() { process.exit(1); // or throw new Error("Uncaught exception"); }, 2000);
debugging,然后删除。
如果您打算以某种方式将其集成到某些testing中,则需要在代码中保持以下行为,并添加一条触发它的方法:
.... // when app is loaded if (process.env.NODE_TEST_TRIGGER_PROCESS_EXIT) { process.exit(1); }
并添加一个执行scheme,以适当的选项运行它:
$ NODE_TEST_TRIGGER_PROCESS_EXIT=true node myapp.js
UPD:如果你真的不关心不同的事件,检查其他信号: https : //nodejs.org/api/process.html#process_signal_events也许应用程序将收到SIGINT或SIGTERM。 但是,如果是SIGKILL,你仍然无法抓住它,所以你必须自己创build事件。