简体   繁体   English

在node.js中执行退出时未显示控制台警告

[英]Console warning not displayed when performing exit in node.js

I am new to Node.js. 我是Node.js的新手。 I have implemented an \\exit link on my site. 我已经在我的网站上实现了\\exit链接。 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. 该页面正确显示,并且node.js服务器已关闭,但未显示控制台警告。 However, calls to console.warn are successful in other parts of the application. 但是,在应用程序的其他部分,对console.warn调用是成功的。 I can see the messages in the output window. 我可以在输出窗口中看到消息。

What could cause this issue? 是什么导致此问题?

Try to delay the process termination: 尝试延迟进程终止:

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

Or using setTimeout : 或使用setTimeout

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

If you don't want to create a new anonymous function, you could use bind: 如果您不想创建新的匿名函数,则可以使用bind:

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

// Later...
process.nextTick(exit);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM