-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
109 lines (101 loc) · 3.19 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
97
98
99
100
101
102
103
104
105
106
107
108
109
const koa = require('koa');
const staticCache = require('koa-static-cache');
const convert = require('koa-convert');
const path = require('path');
const router = require('koa-router')();
const jade = require('jade');
const server = require('./common/server');
const bodyParser = require('koa-bodyparser');
const cors = require('koa-cors');
const co = require('co');
const response_formatter = require('./middlewares/response_formatter');
const fs = require('fs');
//log工具
const logUtil = require('./utils/log_util');
const app = new koa();
let cache = require('./common/cache');
app.use(convert(cors()));
app.use(bodyParser({enableTypes: ['text', 'json', "form"], jsonLimit: '100mb'}));
//静态资源的支持
app.use(staticCache(path.join(__dirname, 'public'), {
maxAge: 365 * 24 * 60 * 60,
dynamic: true
}));
//添加对渲染的缓存支持
cache = new cache(function (name) {
return jade.renderFile(__dirname + '/view/' + name + '.jade');
}, 30 * 24 * 60 * 60);
app.use(async(ctx, next) => {
ctx.render = function (name, data) {
// if (data) {
// this.body = jade.renderFile(__dirname + '/view/' + name + '.jade', data);
// } else {
// this.body = cache.get(name);
// }
this.body = jade.renderFile(__dirname + '/view/' + name + '.jade', data);
};
await next();
});
// logger
app.use(async(ctx, next) => {
//响应开始时间
const start = new Date();
//响应间隔时间
let ms;
try {
//开始进入到下一个中间件
await next();
ms = new Date() - start;
//记录响应日志
logUtil.logResponse(ctx, ms);
} catch (error) {
ms = new Date() - start;
//记录异常日志
logUtil.logError(ctx, error, ms);
}
});
//仅对/api开头的url进行格式化处理
app.use(response_formatter('^/api'));
for (let name in server) {
let fn = server[name];
router.post("/api/" + name, async(ctx, next) => {
ctx.body = await new Promise(function (res, rej) {
co(function*() {
try {
if (ctx.request.url == '/api/upload') {
ctx.request.body = [ctx];
} else if (!(ctx.request.body instanceof Array)) {
let arr = [];
for (let item in ctx.request.body) {
arr.push(ctx.request.body[item]);
}
ctx.request.body = arr;
}
console.log(name, ctx.request.body);
let result = yield fn.apply(fn, ctx.request.body);
res(result);
} catch (e) {
rej(e);
}
});
});
});
}
//页面路由
router.get('/', async(ctx, next) => {
ctx.render('index');
});
let files = fs.readdirSync(__dirname + '/view/');
files.forEach(function (itm) {
itm = itm.split('.')[0];
if (itm != 'layout') {
//登录注册
router.get('/' + itm, async(ctx, next) => {
ctx.render(itm);
});
}
});
app.use(router.routes());
app.listen(4009, function () {
console.log('app start listen ', 4009);
});