-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (53 loc) · 1.55 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
60
61
62
63
64
65
const Koa = require('koa')
const Router = require('koa-router')
const session = require('koa-session2')
const koaStatic = require('koa-static')
const bodyParser = require('koa-bodyparser')
const store = require('./services/store')
const rest = require('./services/restful')
const logger = require('./services/logger')
const routers = require('./services/router')
const routerStop = require('./services/routerStop')
const templating = require('./services/templating')
const templateFilters = require('./services/template-filters')
const config = require('./config')
const app = new Koa();
const router = new Router();
const isProduction = process.env.node_env === 'production';
// session
if(config.redis){
app.use(session({
store: store
}))
}else{
app.use(session({
key: 'sid',
maxAge: 3600*24*15*1000
}))
}
// 访问日志记录
app.use(async (ctx, next) => {
logger.info(ctx.request.path);
await next();
})
// 配置ctx.body解析中间件
app.use(bodyParser())
// 加载静态资源中间件
app.use(koaStatic('.'))
// 加载静态模板中间件
app.use(templating('views', {
noCache: !isProduction,
watch: !isProduction,
filters: templateFilters
}))
// 加载restful API
app.use(rest.restify())
// 未登陆路由拦截
app.use(routerStop())
// 加载路由中间件
var dir = 'routers';
app.use(routers(router, dir));
// 监听启动端口
// app.listen(config.server.port, config.server.host)[好像有个什么bug]
app.listen(config.server.port)
console.log(`app started at port ${config.server.port}...`)