大佬教程收集整理的这篇文章主要介绍了如何停止执行一个node.js脚本?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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.'); }
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.'); }
但是,“正确”的做事方式呢?
备择方案
我也看过使用process.exit()的例子;和process.exit(1);但是给我一个502错误网关错误(我认为是因为它杀死了节点?).
ASIDE:标准回调是第一个参数是错误的函数,如果没有错误,则返回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.'); }
以上是大佬教程为你收集整理的如何停止执行一个node.js脚本?全部内容,希望文章能够帮你解决如何停止执行一个node.js脚本?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。