add(app): 增加路由兜底响应
This commit is contained in:
parent
75ca8e4199
commit
c350edf538
|
@ -6,6 +6,7 @@ import KoaStatic from "koa-static";
|
|||
const parameter = require("koa-parameter");
|
||||
|
||||
require("../config/env.config"); // 加载配置
|
||||
import { captureRouteOmit } from "../middlewares/captureRouteOmit";
|
||||
import { router } from "../routers"; // 加载路由
|
||||
import errHandler from "./errorHandler"; // 加载自定义异常
|
||||
|
||||
|
@ -15,15 +16,16 @@ app.use(
|
|||
KoaBody({
|
||||
multipart: true,
|
||||
formidable: {
|
||||
uploadDir: path.join(__dirname, "../upload"),
|
||||
uploadDir: path.join(__dirname, "../../data/upload"),
|
||||
keepExtensions: true,
|
||||
},
|
||||
parsedMethods: ["POST", "PUT", "PATCH", "DELETE"],
|
||||
})
|
||||
);
|
||||
app.use(KoaStatic(path.join(__dirname, "../upload")));
|
||||
app.use(KoaStatic(path.join(__dirname, "../../public")));
|
||||
app.use(parameter(app));
|
||||
|
||||
app.use(captureRouteOmit);
|
||||
app.use(router.routes()).use(router.allowedMethods());
|
||||
|
||||
// 统一的错误处理
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
export async function captureRouteOmit(ctx: any, next: any) {
|
||||
try {
|
||||
await next();
|
||||
if (!ctx.body) {
|
||||
// 没有资源
|
||||
ctx.status = 404;
|
||||
ctx.body = { message: "Not Found." };
|
||||
}
|
||||
} catch (e) {
|
||||
// 如果后面的代码报错 返回500(泛服务器错误)
|
||||
ctx.status = 500;
|
||||
ctx.body = { message: "Server Error." };
|
||||
// 记录错误日志
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue