![](/img/trans.png)
[英]Is there a way to check if user is logged in using express.Router middleware?
[英]use error middleware when using express.Router
我想在我的應用中使用express.Router。 我有一個運行服務器的文件索引文件。 還有一個文件路由,它運行一些快速路由,這要感謝express.Router。
我想要的是,每當我的一條路由發生故障時,都會到達index中定義的錯誤中間件;
在上面的示例中:-當我使用路由ok時,它起作用-當我使用路由ok時,在未到達錯誤中間件的情況下引發了錯誤。
你知道如何實現嗎?
謝謝 !
https://gist.github.com/VivienAdnot/e3cf44de745531c6cca7be5de53c341a
查看您的代碼,我可以看到您在錯誤處理程序中間件中缺少'next'參數,因為需要'next'才能將控件傳遞到下一個匹配的路由。 只需將index.js中的中間件代碼更改為
app.use((err, req, res, next) => {
console.log('error mw reached');
res.status(500);
res.end();
next();
});
而且有效。
我的錯誤中間件名稱不正確...
// doesn't work
app.use((err, req, res) => {
console.log('error mw reached');
res.status(500);
res.end();
});
至:
//works
app.use((err, req, res, next) => {
console.log('error mw reached');
res.status(500);
res.end();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.