node.js koa2 框架 404 错误抓取跳到指定页面
发布时间:2020-07-03编辑:星王
原创
阅读(188)
node.js koa2 框架 404 错误抓取跳到指定页面
以下是 设置中间件
/异常捕获处理
const handler = async (ctx, next) => {
try {
await next()
} catch (error) {
console.error(error)
console.log("错误"+ctx.status)
ctx.response.body = {
code: '00000',
message: '服务器异常',
desc: error.message
}
}
if(parseInt(ctx.status) === 404 ){
//TODO
//koa2中在这里判断 404的时候跳转或重定向到指定页
ctx.render('404.html');
}
}
// 404异常捕获逻辑,一定要放在第一个中间件
app.use(handler)
关键字:
node