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