2012-05-31 57 views
0

我有一個工作的node.js/express基於服務器,並且使用jade進行模板化。通常沒有問題,但每天我會在請求任何頁面時收到錯誤消息。錯誤是'找不到視圖'。我不知道爲什麼我得到這個錯誤,因爲它只是在幾分鐘前運行良好。然而處理res.render中的玉錯誤()

的問題是我怎麼能強制實施崩潰這一事件,例如:

res.render('index.jade', {info: 'msg'}, function(error, ok) { 
    if (error) 
     throw new Error(''); 
    // Proceed with response 
}; 

我將如何做到這一點?我將如何着手迴應?

謝謝。

+0

你說你想「強迫崩潰」,你的意思是你想退出這個過程嗎? –

+0

你不需要添加擴展名,使用'res.render('index');'' – Donflopez

回答

0

試試這個:

app.use(function (req, res, next) { 
    fs.exists(__dirname + '/views/' + req.url.substring(1) + '.jade', function (exists) { 
     if(!exists) { 
     console.log(err); 
     return next(); 
     } 
     res.render(req.url.substring(1), { title: "No Controller", user: req.session.user }); 
    } 
}); 
1

您可以添加一個錯誤處理的中間件。

app.use(function handleJadeErrors(err, req, res, next) { 
    // identify the errors you care about 
    if (err.message === 'failed to locate view') { 
    // do something sensible, such as logging and then crashing 
    // or returning something more useful to the client 
    } else { 
    // just pass it on to other error middleware 
    next(err); 
    } 
});