2
是否可以使用Express 4向前端發送JSON響應,以指示出現錯誤,並調用Express中間件內的下一個(err)這樣錯誤可以由服務器來處理?還是這些電話完全相互排斥?JSON響應並調用Express中的next()
我現在的假設是,你可以這樣做:
app.get('/', function(req, res, next) {
res.json({ error : true });
});
,你可以這樣做:
app.get('/', function(req, res, next) {
next(new Error('here goes the error message');
});
,但你不能做到這一點
app.get('/', function(req, res, next) {
res.json({ error : true });
next(new Error('here goes the error message');
});
和你不能這樣做:
app.get('/', function(req, res, next) {
next(new Error('here goes the error message');
res.json({ error : true });
});
您能否包含一些示例代碼? – kdbanman
@kdbanman喜歡什麼?它看起來像發送json然後調用'next'。 –
你是什麼意思「這樣錯誤可以由服務器來處理」? – IvanJ