-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (40 loc) · 1.02 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
'use strict'
// mongoose
var fs = require('fs');
var path = require('path');
var mongoose = require('mongoose');
var db = 'mongodb://localhost/imooc-app';
mongoose.Promise = require('bluebird');
mongoose.connect(db);
var models_path = path.join(__dirname, '/app/model');
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);
// koa-server
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());
var router = require('./config/routes')();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(8888);
console.log('listen: 8888');