-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
96 lines (80 loc) · 2.51 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const util = require('util');
const path = require('path');
const escapeHtml = require('escape-html');
const bcklib = require('bcklib');
const Koa = require('koa');
const app = new Koa();
const render = require('koa-ejs');
const json = require('koa-json');
const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser');
const logger = require('koa-logger');
const userAgent = require('koa-useragent');
const mvcrouter = require('koa-mvcrouter');
const koajwt = require('koa-jwt');
const jwtRefresh = require('./lib/middlewares/jwt-refresh');
const appConfig = bcklib.loadConfig();
// 全局异常捕获
app.on('error', (err, ctx) => {
console.error('server error', err);
});
// error handler
onerror(app, {
all: (err, ctx) => {
switch (ctx.accepts('json', 'html')) {
case 'json':
console.log('json err');
ctx.body = JSON.stringify(message.build(true, err.message, {}, err.status));
break;
default:
console.log('html err');
let template = path.join(__dirname, 'views', 'error.html');
template = require('fs').readFileSync(template, 'utf8');
let body = template.replace('{{status}}', escapeHtml(err.status))
.replace('{{stack}}', escapeHtml(err.stack))
.replace('{{message}}', escapeHtml(err.message));
ctx.body = body;
ctx.type = 'html';
break;
}
}
});
// middlewares
//洋葱模型,第一个use用于全局检查,拦截所有请求,可用于权限校验,unless按http method、扩展名、路径忽略验证,支持正则
app.use(koajwt({
secret: appConfig.tokenKey
}).unless({
method: [],
ext: ['.ico', '.css', '.js', '.png', '.jpg'],
path: ['/',
/\/index/, /\/test/, /\/ttt/, /\/jwttest\/login/
]
}));
app.use(jwtRefresh);
//布局及视图配置
render(app, {
//必须设置root目录,否则报错
root: path.join(__dirname, '/views'),
viewExt: 'ejs',
cache: false,
//debug: true
});
app.use(async (ctx, next) => {
//console.log('is httpsync req:', ctx.req.headers['httpsync'] ? true : false);
ctx._appConfig = appConfig;
await next();
ctx.res.setHeader('is_koa_mvc_res', true);
});
//koa中间件
//用于输出控制台KOA请求日志,生产环境可注释掉
app.use(logger());
app.use(userAgent);
app.use(bodyparser({
enableTypes: ['json', 'form', 'text']
}));
app.use(json());
//指定静态文件目录,否则不能静态访问
app.use(require('koa-static')(__dirname + '/public'));
// 注册路由
app.use(mvcrouter.load());
module.exports = app;