app.use("/login", login);
app.use("*", (err: any, req: Request, res: Response, next: NextFunction) => {
console.log('errrorrrr')
res.send('ERRORRRRR4040404040404 ******')
});
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
console.log('errrorrrr')
res.send('ERRORRRRR4040404040404')
});
app.listen(config.port, () => {
console.log(`Running at port ${config.port}`);
});
我在路由之后設定了這兩個錯誤處理程式。我沒有設定 set res.send(),而是Cannot GET /whynowork在我的節點上沒有 console.log 的情況下打開瀏覽器。
如何正確設定404 Error?我試過只放一個,但它仍然回傳Cannot GET /whynowork并且不通過錯誤處理程式。
uj5u.com熱心網友回復:
將此路由器放在您撰寫的最后一個路由器之后,以及在第一個錯誤處理中間件之前(期望err作為第一個引數)
app.all('*', (req: Request, res: Response, next: NextFunction) => {
res.status(404).json({
message: 'hi its 404'
})
})
在你上面寫的情況下,這段代碼應該在login路由器和ERRORERROR...404 ******路由器之間
app.use("/login", login);
app.all('*', (req: Request, res: Response, next: NextFunction) => {
res.status(404).json({
message: 'hi its 404'
})
})
app.use("*", (err: any, req: Request, res: Response, next: NextFunction) => {
console.log('errrorrrr')
res.send('ERRORRRRR4040404040404 ******')
});
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
console.log('errrorrrr')
res.send('ERRORRRRR4040404040404')
});
app.listen(config.port, () => {
console.log(`Running at port ${config.port}`);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/343600.html
