调用 res.send()
后,是否需要调用 return 或以某种方式退出回调函数以确保不再执行任何代码?就像在 PHP 中调用 header 函数时一样,您需要在调用之后调用 exit 以防止进一步的代码被执行。
app.post('/create', function(req, res) {
if(req.headers['x-api-key'] === undefined) {
res.send({msg: "Goodbye"});
}
// other code that should only be processed if it has that header.
});
最佳答案
只需使用返回:
app.post('/create', function(req, res) {
if(req.headers['x-api-key'] === undefined)
return res.send({msg: "Goodbye"});
// other code that should only be processed if it has that header.
});
关于node.js - res.send,之后如何退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165569/