Question

I am new to Node.js. I have implemented an \exit link on my site. When it is browsed, the following function is called:

function exitRequest(req, res) {

    res.setHeader('Content-Type', 'text/html');    
    res.send("<html><body>Bye</body></html>");

    console.warn('%s: Exit requested.', Date(Date.now()) );
    process.exit(0);

};

The page is displayed properly and the node.js server shuts down, but the console warning is not displayed. However, calls to console.warn are successful in other parts of the application. I can see the messages in the output window.

What could cause this issue?

Was it helpful?

Solution

Try to delay the process termination:

console.warn('%s: Exit requested.', Date(Date.now()));
process.nextTick(function() { process.exit(0); });

Or using setTimeout:

setTimeout(function() { process.exit(0); }, 0);

If you don't want to create a new anonymous function, you could use bind:

var exit = process.exit.bind(process, 0);

// Later...
process.nextTick(exit);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top