-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (53 loc) · 1.42 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
var debug = require('debug')('Koa-demo');
var koa = require('koa');
//配置文件
var config = require('./config/config');
var app = koa();
app.use(function *(next){
//config 注入中间件,方便调用配置信息
if(!this.config){
this.config = config;
}
yield next;
});
//log记录
var Logger = require('mini-logger');
var logger = Logger({
dir: config.logDir,
format: 'YYYY-MM-DD-[{category}][.log]'
});
//router use : this.logger.error(new Error(''))
app.context.logger = logger;
var onerror = require('koa-onerror');
onerror(app);
//xtemplate对koa的适配
var xtplApp = require('xtpl/lib/koa');
//xtemplate模板渲染
xtplApp(app,{
//配置模板目录
views: config.viewDir
});
var session = require('koa-session');
app.use(session(app));
//post body 解析
var bodyParser = require('koa-bodyparser');
app.use(bodyParser());
//数据校验
var validator = require('koa-validator');
app.use(validator());
//静态文件cache
var staticCache = require('koa-static-cache');
var staticDir = config.staticDir;
app.use(staticCache(staticDir+'/js'));
app.use(staticCache(staticDir+'/css'));
//路由
var router = require('koa-router');
app.use(router(app));
//应用路由
var appRouter = require('./router/index');
appRouter(app);
var usersRouter = require('./router/users');
usersRouter(app);
app.listen(config.port);
console.log('listening on port %s',config.port);
module.exports = app;