调用res.send()
后,是否需要调用返回或以某种方式退出回调函数以确保不执行其他代码?就像在PHP中调用头函数时一样,您需要在此之后调用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.
});
答案 0 :(得分:8)
只需使用return:
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.
});
答案 1 :(得分:0)
根据节点手册:
必须在每个响应上调用方法response.end()。
答案 2 :(得分:-1)
始终使用next()。
app.post('/create', function(req, res, next) {
if(req.headers['x-api-key'] === undefined) {
req.send({msg: "Goodbye"});
return next();
}
// other code that should only be processed if it has that header.
});