-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (51 loc) · 1.46 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
'use strict';
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const db = 'mongodb://localhost/test';
/* 连接数据库 */
mongoose.Promise = require('bluebird');
mongoose.connect(db, {useMongoClient: true});
/**
* 获取数据库表对应的js对象所在的路径
* @type {[type]}
*/
const models_path = path.join(__dirname, '/app/models');
/**
* 已递归的形式,读取models文件夹下的js模型文件,并require
* @param {[type]} modelPath [description]
* @return {[type]} [description]
*/
let walk = function (modelPath) {
fs.readdirSync(modelPath).forEach(function (file) {
let filePath = path.join(modelPath, '/' + file)
let stat = fs.statSync(filePath)
if (stat.isFile()) {
if (/(.*)\.(js|coffee)/.test(file)) {
require(filePath)
}
}
else if (stat.isDirectory()) {
walk(filePath)
}
})
};
walk(models_path);
require('babel-register');
const Koa = require('koa');
const logger = require('koa-logger');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.use(logger());
app.use(session(app));
app.use(bodyParser());
/**
* 使用路由转发请求
* @type {[type]}
*/
const router = require('./config/router')();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
console.log('app started at port 3000...');