如何停止执行 node.js 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22364400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:01:15  来源:igfitidea点击:

How to stop execution of a node.js script?

node.jsexit

提问by AJB

Say I've got this script:

说我有这个脚本:

var thisIsTrue = false;

exports.test = function(request,response){

    if(thisIsTrue){
        response.send('All is good!');
    }else{
        response.send('ERROR! ERROR!');
        // Stop script execution here.
    }

    console.log('I do not want this to happen if there is an error.');

}

And as you can see, I'd like to stop the script from executing any downstream functions if there's an error.

如您所见,如果出现错误,我想阻止脚本执行任何下游函数。

I've managed to achieve this by adding return;after the error response is sent:

我设法通过return;在发送错误响应后添加来实现这一点:

var thisIsTrue = false;

exports.test = function(request,response){

    if(thisIsTrue){
        response.send('All is good!');
    }else{
        response.send('ERROR! ERROR!');
        return;
    }

    console.log('I do not want this to happen if there is an error.');

}

But is that the 'correct' way to do things?

但这是做事的“正确”方式吗?

Alternatives

备择方案

I've also seen examples that use process.exit();and process.exit(1);, but that gives me a 502 Bad Gatewayerror (I assume because it kills node?).

我也看过使用process.exit();and 的例子process.exit(1);,但这给了我一个502 Bad Gateway错误(我假设是因为它杀死了节点?)。

And callback();, which just gave me an 'undefined' error.

而且callback();,这只是给了我一个“未定义”的错误。

What is the 'correct' way to stop a node.js script at any given point and prevent any downstream functions from executing?

在任何给定点停止 node.js 脚本并阻止任何下游函数执行的“正确”方法是什么?

回答by Tim Brown

Using a returnis the correct way to stop a function executing. You are correct in that process.exit()would kill the whole node process, rather than just stopping that individual function. Even if you are using a callback function, you'd want to return it to stop the function execution.

使用 areturn是停止函数执行的正确方法。您是正确的,这process.exit()会杀死整个节点进程,而不仅仅是停止该单个功能。即使您正在使用回调函数,您也希望返回它以停止函数执行。

ASIDE: The standard callback is a function where the first argument is an error, or null if there was no error, so if you were using a callback the above would look like:

旁白:标准回调是一个函数,其中第一个参数是错误,如果没有错误,则为 null,因此如果您使用的是回调,则上述内容如下所示:

var thisIsTrue = false;

exports.test = function(request, response, cb){

    if (thisIsTrue) {
        response.send('All is good!');
        cb(null, response)
    } else {
        response.send('ERROR! ERROR!');
        return cb("THIS ISN'T TRUE!");
    }

    console.log('I do not want this to happen. If there is an error.');

}

回答by mate.gwozdz

You should use return, which will help you respond to what happened. Here's a bit cleaner version, basically first validate whatever you want to validate, rather than encapsulating everything in if{}else{} statements

您应该使用return,这将帮助您对发生的事情做出反应。这是一个更简洁的版本,基本上首先验证您要验证的任何内容,而不是将所有内容封装在 if{}else{} 语句中

exports.test = function(request, response, cb){

    if (!thisIsTrue) {
        response.send('ERROR! ERROR!');
        return cb("THIS ISN'T TRUE!");
    }

    response.send('All is good!');
    cb(null, response)

    console.log('I do not want this to happen. If there is an error.');

}

Another way would be to use throw

另一种方法是使用 throw

exports.test = function(request, response, cb){

    if (!thisIsTrue) {
        response.send('ERROR! ERROR!');
        cb("THIS ISN'T TRUE!");
        throw 'This isn\'t true, perhaps it should';
    }

    response.send('All is good!');
    cb(null, response)

    console.log('I do not want this to happen. If there is an error.');

}

Finally, examples that would stop entire app from further execution:

最后,会阻止整个应用程序进一步执行的示例:

a) Throw an error, which will also help you debug the app (wouldn't completely stop the app, if the test()function was wrapper in try{}catch(e){}):

a) 抛出一个错误,这也将帮助您调试应用程序(不会完全停止应用程序,如果test()函数在 中包装try{}catch(e){}):

throw new Error('Something went wrong')

throw new Error('Something went wrong')

b) Stop script execution (works with Node.js):

b) 停止脚本执行(适用于 Node.js):

process.exit()

process.exit()