-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (46 loc) · 1.25 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
'use strict';
var app = require('koa')();
var logger = require('koa-logger')();
var parse = require('co-body');
var error = require('koa-error');
var config = require('./config/config');
app.use(function *(next){
//config 注入中间件,方便调用配置信息
if(!this.config){
this.config = config;
}
yield next;
});
// print req&res logger on terminal console
app.use(logger);
// onerror show page
app.use(error({
engine: 'lodash',
template: './error.html'
}));
//xtemplate对koa的适配
var xtplApp = require('xtpl/lib/koa');
//xtemplate模板渲染
xtplApp(app,{
//配置模板目录
views: config.viewDir
});
/*app.use(require('koa-static')(__dirname + '/public'));
//静态文件cache
var staticCache = require('koa-static-cache');
var staticDir = config.staticDir;
app.use(staticCache(path.join(staticDir, 'js')));
app.use(staticCache(path.join(staticDir, 'css')));*/
//路由
var router = require('koa-router');
app.use(router(app));
app.use(function* (next){
if(typeof this.path === 'undefined')
yield next;
var appRouter = require('./router/index');
appRouter.appRouter.call(this, app, this.path);
})
var port = process.env.PORT || 3000;
app.listen(port);
console.log('listening on port %s', port);
module.exports = app;