-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (40 loc) · 1.37 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
var express = require('express');
var path = require('path');
var port = process.env.PORT || 3000;
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bodyParser = require('body-parser'); //做文件解析,格式化表单数据
var mongoStore = require('connect-mongo')(session);
var mongoose = require('mongoose');
var logger = require('morgan');
var serverStatic = require('serve-static');
var dbUrl = 'mongodb://localhost:27017/imovie';
var app = express();
app.locals.moment = require('moment'); //格式化日期
mongoose.Promise = global.Promise;
mongoose.connect(dbUrl,{useMongoClient: true});
console.log('MongoDB connect success!');
app.use(session({
secret: 'imooc',
store: new mongoStore({
url: dbUrl,
db: 'sessions'
}),
resave: false,
saveUninitialized: true
}))
var env = process.env.NODE_ENV ||'development';
if('development' === env){
app.set('showStackError',true);
app.use(logger(':method :url :status'));
app.locals.pretty = true;
mongoose.set('debug',true);
}
app.listen(port, function(){
console.log('server start on ' + port);
});
app.use(bodyParser.urlencoded({extended:true}));
app.use(serverStatic(path.join(__dirname,'public'))); //配置静态文件,页面所需要的文件都放在public文件夹下
app.set('views','./app/views/pages');
app.set('view engine','pug');
require('./config/routes')(app);