如何仅停止当前页面进程,而不是整个NodeJS服务器

How to stop current page process only, not the whole NodeJS server

本文关键字:服务器 NodeJS 何仅停 当前页 进程      更新时间:2023-09-26

这个问题可能听起来很笨拙,因为我不知道如何用语言表达。请看下面的例子:

//PHP
<?php
    die( "The PHP process stops here. The PHP is still running" );
    insert_into_database_someinfo(); //This function will not be executed
?>

我的问题是:什么是等效的死()或exit()在PHP的Nodejs, ,而不停止整个当前Nodejs服务器?我发现这个链接的所有者有同样的问题,但使用process.exit()的建议是不合适的。

process.exit()将停止整个服务器。

下面的代码尝试输出到浏览器,但与PHP die()不同的是,这将继续到下一行

req.end( 'This is an output to the browser' );
insert_into_database_someinfo(); //This function will still be executed.

我认为这有点类似于PHP中的echo(),因为一切都在继续。我想输出信息,然后在那里停止进程(只是当前请求,而不是整个nodejs服务器)。

请建议。

插入return来停止函数的执行:

if (nogood) {
    req.end( 'This is an output to the browser' );
    return;
}
insert_into_database_someinfo();