-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
57 lines (46 loc) · 1.19 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
'use strict'
var fs = require('fs')
var path = require('path')
var mongoose = require('mongoose')
var cors = require('koa-cors')
var db = 'database-url'
mongoose.Promise = require('bluebird')
mongoose.connect(db)
mongoose.connection.on("connected",function(){
console.log("数据库连接成功")
})
var models_path = path.join(__dirname, '/app/models')
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 logger = require('koa-logger')
var session = require('koa-session')
var bodyParser = require('koa-bodyparser')
var app = koa()
app.keys = ['imooc']
app.use(logger())
app.use(session(app))
app.use(bodyParser())
// 解决跨域问题
app.use(cors())
var router = require('./config/routes')()
app
.use(router.routes())
.use(router.allowedMethods())
app.listen(8084)
console.log('Listening: http://localhost/8084')