-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (54 loc) · 1.3 KB
/
index.js
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
const Koa = require("koa");
const app = new Koa();
const bodyParser = require("koa-bodyparser");
const router = require("./router");
const nunjucks = require("koa-nunjucks-2");
const path = require("path");
const koaStatic = require("koa-static");
const util = require("./util/util");
const axios = require("axios");
app.use(koaStatic(path.resolve(__dirname, "public")));
app.use(
nunjucks({
ext: "html",
path: path.join(__dirname, "views"),
nunjucksConfig: {
trimBlocks: true,
},
})
);
app.use(bodyParser());
// 鉴权中间件
app.use(async (ctx, next) => {
let _match =
["/login", "/qrcode", "/token", "/check"].indexOf(ctx.request.path) >= 0;
if (!_match) {
let token = util.getToken(ctx);
if (!token) {
util.redirectToLogin(ctx);
} else {
let res = await axios.get("https://api.syreo.cn/my", {
headers: { "x-session": token },
});
if (res.data.data && res.data.data.isAdmin) {
ctx.state.token = token;
await next();
} else {
util.redirectToLogin(ctx);
}
}
} else {
await next();
}
});
// 错误处理
app.use(async (ctx, next) => {
try {
await next();
} catch (error) {
console.log("error", error.message);
}
});
router(app);
app.listen(5001);
console.log("app start at 5001");