-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
65 lines (60 loc) · 2.13 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
var express = require("express");
var mongoose=require("mongoose");
mongoose.Promise = global.Promise = require('bluebird');
var path=require("path");
var bodyParser = require('body-parser')
var cookiParser=require("cookie-parser");
var logger = require('morgan');
var multipart=require("connect-multiparty");
var session=require("express-session");
var mongoStore=require("connect-mongo")(session);
var port=process.env.PORT||3000;
var env=process.env.NODE_ENV||"development";
var dbUrl="mongodb://localhost/graduationDesign";
var app = express();
app.listen(port);
var indexRouter = require('./routes/indexRouter');
var userRouter = require('./routes/userRouter');
var bookRouter = require('./routes/bookRouter')
// 连接数据库
mongoose.connect(dbUrl);
mongoose.connection.on('connected', function () {
console.log('Mongoose connection open to ' + dbUrl);
});
mongoose.connection.on('error',function (err) {
console.log('Mongoose connection error: ' + err);
});
mongoose.connection.on('disconnected', function () {
console.log('Mongoose connection disconnected');
});
// 公用中间件
app.set("views","./app/views/pages");
app.set("view engine","pug");
app.use(express.static(path.join(__dirname,"public")));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(multipart());//用于文件上传
app.use(cookiParser());//session中间件依赖于cookieParser
app.use(session({
secret:"graduationDesign",
store:new mongoStore({
url:dbUrl,
collection:"sessions"
})
}))
app.locals.moment=require("moment");
app.use(function(req,res,next){
res.locals.user = req.session.user || null;
next();
})
// 路由中间件
app.use(indexRouter);
app.use(userRouter);
app.use(bookRouter);
if("development"===env){//如果env为本地开发环境
app.set("showStackError",true)//报错的时候,将其打印出来
app.use(logger(":method :url :status"))//显示请求的类型、路径、状态
app.locals.pretty=true //页面的源码由压缩转为格式化后,提高可读性
mongoose.set('debug', true)//开启debug模式,可以看到完整的执行过程
}
module.exports=app