-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
204 lines (144 loc) · 3.5 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//需要安装 express mongoose underscore moment
var express = require('express');
var path = require('path');
var router = express.Router();
var mongoose = require('mongoose'); //加载mongoose模块
var bodyParser = require('body-parser'); //引入body-parser,用于接收数据等
var _ = require('underscore');
var Movie =require('./models/movie'); // 加载模型
var port = process.env.PORT || 3000; //端口号
var app = express();
//连接本地数据库
// mongoose.connect('mongodb://localhost/imooc');
//连接远程数据库
//格式 mongoose.connect('mongodb://user:pwd@localhost:port/db');
mongoose.connect('mongodb://imooc:[email protected]:27000/imooc');
app.set('views','views/pages'); //模版引擎目录
app.set('view engine', 'jade'); //模版引擎
app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// app.use(multer());
app.use(express.static(path.join(__dirname, 'public'))); //bower 静态资源路径
app.locals.moment = require('moment'); //格式化时间模块
app.listen(port);
console.log('server is listing port ' + port);
app.get('/', function(req, res){
// 查询数据库
Movie.fetch(function(err, movies){
if(err){
console.log(err);
}
res.render('index',
{
title: '首页',
movies : movies
}
);
});
})
app.get('/movie/:id', function(req, res){
var id = req.params.id;
Movie.findById(id, function(err,movie){
res.render('detail',
{
title : '详情页',
movie : movie
}
);
});
});
//获取post数据
app.post('/admin/movie/new', function(req, res){
console.log(req.body);
var id = req.body.movie._id;
var movieObj = req.body.movie;
var _movie;
if(id !== 'undefined'){
Movie.findById(id, function(err, movie){
if(err){
console.log(err);
}
_movie = _.extend(movie, movieObj);
_movie.save(function(err, movie){
if(err){
console.log(err);
}
res.redirect('/movie/' + movie._id);
});
});
}
else{
_movie = new Movie({
doctor : movieObj.doctor,
title : movieObj.title,
country :movieObj.country,
language:movieObj.language,
year:movieObj.year,
poster:movieObj.poster,
summary:movieObj.summary
});
_movie.save(function(err, movie){
if(err){
console.log(err);
}
res.redirect('/movie/' + movie._id);
});
}
});
app.get('/admin/movie', function(req, res){
res.render('admin',
{
title : '详情页',
movie : {
title : '',
poster : '',
doctor : '',
country : '',
language : '',
year : '',
summary : ''
}
}
);
})
//更新电影
app.get('/admin/update/:id',function(req, res){
var id = req.params.id;
if(id){
Movie.findById(id, function(err, movie){
res.render('admin', {
title : 'imooc 更新页面',
movie: movie
});
});
}
});
//列表页删除
app.delete('/admin/list', function(req, res){
var id = req.query.id;
if(id){
Movie.remove({_id : id}, function(err, movie){
if(err){
console.log(err);
}
else{
res.json({success: 1});
}
})
}
})
app.get('/admin/list', function(req, res){
Movie.fetch(function(err, movies){
if(err){
console.log(err);
}
console.log(movies[0].meta.createAt);
res.render('list',
{
title: '列表页',
movie : movies
}
);
});
})