-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (51 loc) · 1.82 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
const fs = require('fs');
const path = require('path');
const mongoose = require('mongoose');
const db = 'mongodb://localhost/imooc-app';
mongoose.Promise = require('bluebird');
// mongoose.Promise = global.Promise;
mongoose.connect(db);
// require('./app/models/user');
let models_path = path.join(__dirname, '/app/models');
let walk = function (modelPath) {
fs
.readdirSync(modelPath)
.forEach(function (file) {
let filePath = path.join(modelPath, '/' + file);
// retrieve information about the file pointed to by pathname
let stat = fs.statSync(filePath);
if (stat.isFile()) {
// The test() method executes a search for a match
// between a regular expression and a specified string.
// Returns true or false.
// / regular expression /
// preceding the decimal point with \ means
// the pattern must look for the literal character '.'
if (/(.*)\.(js|coffee)/.test(file)) {
require(filePath);
}
} else if (stat.isDirectory()) {
// recursion use walk
walk(filePath);
}
})
};
walk(models_path);
const Koa = require('koa');
const logger = require('koa-logger');
const session = require('koa-session');
const bodyParser = require('koa-bodyparser');
const router = require('./config/routes');
const app = new Koa();
app.keys = ['immoc'];
// Development style logger middleware
app.use(logger());
// if you prefer all default config, just use => app.use(session(app))
app.use(session(app));
// the parsed body will store in ctx.request.body
app.use(bodyParser());
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
console.log('Listening: 3000');