-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·66 lines (56 loc) · 1.92 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
64
65
66
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var session = require('express-session')
var mongoose =require('mongoose');
var mongoStore=require('connect-mongo')(session);
var bodyParser = require('body-parser');
var port = process.env.PORT || 3000;
var index = require('./routes/index');
var app = express();
app.locals.moment = require('moment');
mongoose.Promise = global.Promise
var dbUrl='mongodb://127.0.0.1:27017/movie';
mongoose.connect(dbUrl);
// view engine setup
app.set('views', path.join(__dirname, '/app/views/pages'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//bodyParser.json 与 bodyParser.urlencoded 的解析功能
//bodyParser.json是用来解析json数据格式的。
//bodyParser.urlencoded则是用来解析我们通常的form表单提交的数据,也就是请求头中包含这样的信息: Content-Type: application/x-www-form-urlencoded
// app.use(express.cookieParser());
// app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(session());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret:'imooc',
store:new mongoStore({
url:dbUrl,
collection:'session',
}),
resave:false,
saveUninitialized:true
}));
//配置 生产 开发环境
if('development'===app.get('env')){
app.set('showStackError',true);//在屏幕上获取error
app.use(logger(':method :url :status')); //log配置
app.locals.pretty=true; //格式化
mongoose.set('debug',true);
}
//路由
app.use('/', index);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// app.listen(port);
console.log('imooc started on port ' + port);
module.exports = app;