index.js
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import path from "path";
import bodyParser from "koa-bodyparser";
import staticFiles from "koa-static";
import Rule from "./rule";
import Send from "./send";
import Auth from "./auth";
import Log from "./log";
import Func from "./func";
export default (app) => {
//缓存拦截器
app.use(async (ctx, next) => {
if (ctx.url == "/favicon.ico") return;
await next();
ctx.status = 200;
ctx.set("Cache-Control", "must-revalidation");
if (ctx.fresh) {
ctx.status = 304;
return;
}
});
// 日志中间件
app.use(Log());
// 数据返回的封装
app.use(Send());
// 方法封装
app.use(Func());
//权限中间件
app.use(Auth());
//post请求中间件
app.use(bodyParser());
//静态文件中间件
app.use(staticFiles(path.resolve(__dirname, "../../../public")));
// 规则中间件
Rule({
app,
rules: [
{
path: path.join(__dirname, "../controller/admin"),
name: "admin",
},
{
path: path.join(__dirname, "../controller/client"),
name: "client",
},
],
});
// 增加错误的监听处理
app.on("error", (err, ctx) => {
if (ctx && !ctx.headerSent && ctx.status < 500) {
ctx.status = 500;
}
if (ctx && ctx.log && ctx.log.error) {
if (!ctx.state.logged) {
ctx.log.error(err.stack);
}
}
});
};