を使用することができます。どこでもあなたは今、単にvar newError = new Error('my error message');
でエラーを作成し、エラーがスローされた後に実行を停止するにはnext(newError);
でnext
コールバックに渡し、応答を送信しないようにすることができ、アプリ内
二回使用
return next (newError);
// example route
app.get('/test', function(req, res, next){
if (someProblem) {
var newError = new Error('oops');
return next(newError);
}
res.send('all OK - no errors');
});
// attach this errorHandler just before you create the server
app.use(function(error, req, res, next){
console.log(error);
res.status(500).json(error);
});
// this is where you create the server
var server = app.listen(4000, function(){
console.log('server started');
});
あなたが次の電話をした場合にのみチェーンが起こります。それ以外の場合は停止します – Simon
あなたは正しいです。ありがとうございます – Lev