When to use next() and return next() in Node.js

简介: Some people always write return next() is to ensure that the execution stops after triggering the callback.

Some people always write return next() is to ensure that the execution stops after triggering the callback.

If you don't do it, you risk triggering the callback a second time later, which usually has devastating results. Your code is fine as it is, but I would rewrite it as:

app.get('/users/:id?', function(req, res, next){
    var id = req.params.id;

    if(!id)
        return next();

    // do something
});

It saves me an indentation level, and when I read the code again later, I'm sure there is no way next is called twice.

http://stackoverflow.com/questions/16810449/when-to-use-next-and-return-next-in-node-js

 

目录
打赏
0
1
1
1
97
分享
相关文章
mapbox没有token/token失效,地图闪烁后变空白,报错Error: A valid Mapbox access token is required to use Mapbox GL JS.
本博客介绍了mapbox如何去除token验证,暴力破解mapbox的token验证机制。一劳永逸解决mapbox地图闪现一下然后变成空白,报错Error: A valid Mapbox access token is required to use Mapbox GL JS.的方法,还介绍了类似问题的具体解决思路。 只有锻炼思维才能可持续地解决问题,只有思维才是真正值得学习和分享的核心要素。如果这篇博客能给您带来一点帮助,麻烦您点个赞支持一下,还可以收藏起来以备不时之需,有疑问和错误欢迎在评论区指出~
鸿蒙next版开发:分析JS Crash(进程崩溃)
在HarmonyOS 5.0中,JS Crash指未处理的JavaScript异常导致应用意外退出。本文详细介绍如何分析JS Crash,包括异常捕获、日志分析和典型案例,帮助开发者定位问题、修复错误,提升应用稳定性。通过DevEco Studio收集日志,结合HiChecker工具,有效解决JS Crash问题。
309 4
JavaScript 严格模式(use strict)
JavaScript 严格模式(use strict)
109 0
Next js:点击登录显示登录表单,点击注册显示注册表单的功能
本文提供了一个Next.js中使用React状态管理实现点击按钮切换显示登录和注册表单的功能示例,包括创建`authform.tsx`组件和在页面组件中引入使用的方法。
JavaScript 函数中break,continue,return 的区别
JavaScript 函数中break,continue,return 的区别
82 0
next.js博客搭建_初始化next项目(第一步)
next.js博客搭建_初始化next项目(第一步)
171 1
js代码中“use strict” 是什么意思? 使用它的区别是什么?
js代码中“use strict” 是什么意思? 使用它的区别是什么?
184 1
js代码中“use strict” 是什么意思? 使用它的区别是什么
js代码中“use strict” 是什么意思? 使用它的区别是什么
188 1

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等