admin管理员组文章数量:1394069
So I'm currently learning node.js and express, and I encountered this:
exports.checkBody = checkBody = (req, res, next) => {
if (!req.body.name || !req.body.price) {
// Return statement is here
return res.status(400).json({
status: "fail",
message: "Please add a name and price"
})
}
next();
}
Why do I need to put the return statement here, because when I remove the return, it works exactly the same.
Here is the routes
router.post(tourController.checkBody, tourController.createTour);
So I'm currently learning node.js and express, and I encountered this:
exports.checkBody = checkBody = (req, res, next) => {
if (!req.body.name || !req.body.price) {
// Return statement is here
return res.status(400).json({
status: "fail",
message: "Please add a name and price"
})
}
next();
}
Why do I need to put the return statement here, because when I remove the return, it works exactly the same.
Here is the routes
router.post(tourController.checkBody, tourController.createTour);
Share
Improve this question
edited Aug 14, 2020 at 18:10
Barmar
784k57 gold badges548 silver badges659 bronze badges
asked Aug 14, 2020 at 18:08
TidrisTidris
3835 silver badges11 bronze badges
3
- So you are wondering why you would not check to see if input is valid and return an error if something is not set? return exits.... – epascarello Commented Aug 14, 2020 at 18:10
-
If you remove the
return
statement, how do you send the error message back to the client? – Barmar Commented Aug 14, 2020 at 18:11 -
@Barmar: With
res.status(400).json(…)
. – Ry- ♦ Commented Aug 14, 2020 at 18:11
3 Answers
Reset to default 5The reason for the return
is so you don't execute next()
if an error is detected.
The return value isn't important, because res.status()
sends the response to the client. So this is actually just a shorthand for doing
exports.checkBody = checkBody = (req, res, next) => {
if (!req.body.name || !req.body.price) {
res.status(400).json({
status: "fail",
message: "Please add a name and price"
});
return;
}
next();
}
The return
is used for the effect of exiting the function in this case, not for usefully returning a value. It’s a shorter equivalent that some people prefer as a style choice over this:
res.status(400).json({
status: "fail",
message: "Please add a name and price"
})
return
If you remove the return
entirely, next()
will be called, and the next handler will run, which usually isn’t correct after you’ve already responded, and especially not in this case where the middleware being implemented is validation that’s supposed to stop the route from running when validation fails.
I personally prefer the more explicit separate return
for the purposes of avoiding exactly this type of confusion, but it doesn’t e up as much anymore now that promises and async + await are available as options.
Because return
stops the execution of the function. If there is no return you send the response back but that doesnt mean that the function stops to execute.
Imagine you have some kind of auth middleware that checks if an user is valid and you either send an error back or you move to the next Middleware with next()
Now you dont use return
. You get an response on the client that you got rejected but on the serverside the code still got executed. I hope you get what i mean. Its maybe not the best example but... Yea..
本文标签: javascriptWhy do we return the status code and response in expressStack Overflow
版权声明:本文标题:javascript - Why do we return the status code and response in express? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744665458a2618504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论