Quitting node.js gracefully

后端 未结 5 630
天涯浪人
天涯浪人 2020-11-30 19:27

I\'m reading through the excellent online book http://nodebeginner.org/ and trying out the simple code

var http = require(\"http\");

function onRequest(req         


        
5条回答
  •  眼角桃花
    2020-11-30 20:01

    As node.js is an event-driven runtime the most graceful exit is to exhaust the queue of pending events. When the event queue is empty the process will end. You can ensure the event queue is drained by doing things such as clearing any interval timers that are set and by closing down any servers with open socket connections. It gets trickier when using 3rd party modules because you are at the mercy of whether the module author has taken care to gracefully drain the pending events it created. This might not be the most practical way to exit a node.js process as you will spend a lot of effort tracking down 'leaked' pending events, but it is the most graceful I think.

提交回复
热议问题