-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (46 loc) · 1.24 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
var bodyParser = require('koa-bodyparser');
var compress = require('koa-compress');
var render = require('./render');
var router = require('./router');
var getControllers = require('./lib/getControllers');
var getViews = require('./lib/getViews');
var init = require('./lib/init');
module.exports = function (app, config) {
if(!config){
config = {};
}
if(!config.root){
config.root = process.cwd();
}
getControllers(config);
getViews(config);
app.use(async(ctx, next) => {
await init.call(ctx, config);
await next();
});
app.on("error", (err, ctx) => {
if (ctx) {
ctx.status = 500;
}
console.log(ctx.url);
console.log(err.stack);
});
app.use(bodyParser());
app.use(compress({
filter: function (content_type) {
return /text/i.test(content_type)
},
threshold: 2048,
flush: require('zlib').Z_SYNC_FLUSH
}));
// 提供render方法-
app.use(async(ctx, next) => {
await render.call(ctx, config);
await next();
});
// 找到controller,执行action
app.use(async(ctx, next) => {
await router.call(ctx, config);
await next();
});
};