-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
68 lines (64 loc) · 1.99 KB
/
Copy pathapp.js
File metadata and controls
68 lines (64 loc) · 1.99 KB
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
var express = require('express');
var path = require('path');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var multipart = require('connect-multiparty');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var mongoStore = require('connect-mongo')(session);
var logger = require('morgan');
var port = process.env.PORT || 3000;
var app = express();
app.locals.moment = require('moment');
var fs = require('graceful-fs')
var dbUrl = 'mongodb://localhost/imooc';
mongoose.connect(dbUrl);
// models loading
var models_path = __dirname + '/app/models'
var walk = function(path) {
fs
.readdirSync(path)
.forEach(function(file) {
var newPath = path + '/' + file
var stat = fs.statSync(newPath)
if (stat.isFile()) {
if (/(.*)\.(js|coffee)/.test(file)) {
require(newPath)
}
}
else if (stat.isDirectory()) {
walk(newPath)
}
})
}
walk(models_path)
// 静态资源请求路径
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname,'bower_components')));
// console.info('__dirname',__dirname,path.join(__dirname, 'bower_components'));
app.set('views','./app/views/pages');
app.set('view engine','jade');
// app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multipart());
app.use(cookieParser());
app.use(session({
secret: 'imooc',
store: new mongoStore({
url: dbUrl,
collection:'sessions'
}),
proxy: true,
resave: true,
saveUninitialized: true
}));
if('development' === app.get('env')){
app.set('showStackError', true); //输出报错信息
app.use(logger(':method :url :status')); //输出的信息领域
app.locals.pretty = true; //输出样式格式化、便于观看
mongoose.set('debug', true); //数据库层输出报错信息
}
require('./config/routes')(app)
app.listen(port);
console.log('imooc started on port '+port);