-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (55 loc) · 1.76 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
/**
* Created by Yooz on 2016/11/23.
*/
var express = require('express')
var path = require("path")
var morgan = require('morgan')
var bodyParser = require("body-parser")
var mongoose = require("mongoose");
//两个保存会话状态的中间件
var cookieParser = require("cookie-parser")
var cookieSession = require("cookie-session");
//持久化session中间件
const session = require('express-session');
var mongoStore = require("connect-mongo")(session)
var app = express()
var port = process.env.PORT || 3000
var dbUrl = "mongodb://localhost/yooz"
//连接数据库
mongoose.connect(dbUrl);
app.use(cookieParser())
app.use(cookieSession({
secret: 'yooz',
// 持久化session
store: new mongoStore({
url: dbUrl,
collection: "sessions",
resave: false,
saveUninitialized: true
})
}))
app.set("views", './app/views/pages') //设置视图根目录
app.set('view engine', 'ejs') //设置视图的模版引擎
// app.use(express.static(path.join(__dirname, 'bower_components')))
app.use(express.static(path.join(__dirname, 'public')))
//提交数据转换对象中间件
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
//上传图片文件的中间件
app.use(require('connect-multiparty')())
var moment = require("moment");
app.listen(port);
console.log("成功启动:" + port)
//在开发环境的时候
if ("development" === app.get('env')) {
//在控制台输出信息
app.set('showStackError', true);
//想看到的信息, 请求的类型, 请求的url路径, 请求的状态。
app.use(morgan(':method :url :status'))
//格式化代码。
app.locals.pretty = true
mongoose.set('debug', true)
}
//需要放在最后面
//导入路由,传入app
require('./config/routes')(app)