forked from MuYunyun/yunban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (71 loc) · 2.09 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
'use strict';
var Koa = require('koa');
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var dbUrl = 'mongodb://localhost/imooc';
mongoose.connect(dbUrl);
//models loading
var models_path = __dirname + '/app/models';
var walk = function(path) { //遍历这个目录
fs
.readdirSync(path)
.forEach(function(file) {
var newPath = path + '/' + file;
var stat = fs.statSync(newPath);
if (stat.isFile()) {
if (/(.*)\.(js|coffee)/.test(file)) {
require(newPath);
}
}
else if (stat.isDirectory()) {
walk(newPath);
}
});
};
walk(models_path);
var menu = require('./wx/menu');
var wx = require('./wx/index.js');
var wechatApi = wx.getWechat();
wechatApi.deleteMenu().then(function() {
return wechatApi.createMenu(menu);
})
.then(function(msg) {
console.log(msg);
});
var app = new Koa();
var Router = require('koa-router'); //引人路由模块
var router = new Router(); //拿到一个路由的实例
var session = require('koa-session');
var bodyParser = require('koa-bodyparser'); //解析post过来的数据
var User = mongoose.model('User');
var views = require('koa-views');
var moment = require('moment');
app.use(views(__dirname + '/app/views', {
extension: 'jade',
locals: {
moment: moment
}
}));
app.use(require('koa-static')(__dirname + '/public')); //设置静态文件目录
app.keys = ['imooc']; //设置session的keys
app.use(session(app)); //传入session的中间件 用cookie实现用户的会话状态
app.use(bodyParser());
app.use(function *(next) { //预处理用户的信息来作同步
var user = this.session.user;
if (user && user._id) {
this.session.user = yield User.findOne({_id: user._id}).exec();
this.state.user = this.session.user; //每个模板渲染的时候都会读到state
}
else {
this.state.user = null;
}
yield next;
});
require('./config/routes')(router);
app
.use(router.routes()) //让路由规则生效
.use(router.allowedMethods());
app.listen(1234);
console.log('Listening 1234');