-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
133 lines (119 loc) · 3.11 KB
/
Copy pathapp.js
File metadata and controls
133 lines (119 loc) · 3.11 KB
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
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongooes = require('mongoose');
var Movie = require('./models/movie');
var _ = require('underscore');
var port = process.env.PORT || 3000;
var app = express();
mongooes.Promise = global.Promise;
mongooes.connect('mongodb://localhost/movie');
app.set('views', './views/pages');
app.set('view engine', 'jade');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'bower_components')));
app.locals.moment=require('moment');
app.listen(port);
console.log('test start 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.title + ' 详情',
movie: movie
});
});
});
// 后台
app.get('/admin/movie', function (req, res) {
console.log('进入后台');
res.render('admin', {
title: '后台页',
movie: {
title: '',
flash: '',
doctor: '',
country: '',
language: '',
year: '',
poster: '',
summary: ''
},
});
});
//录入时新增或者更新
app.get('/admin/movie/new', function (req, res) {
var id = req.body.movie._id;
var movieOBJ = req.body.movie;
console.log('获取的数据:'+movieOBJ);
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) {
console.log('查询错误,错误如下: ' + err);
});
res.redirect('/movie/' + movie._id);
});
} else {
_movie = new Movie({
doctor: movieOBJ.doctor,
title: movieOBJ.title,
country: movieOBJ.country,
flash: movieOBJ.flash,
year: movieOBJ.year,
summary: movieOBJ.summary,
language: movieOBJ.language,
poster: movieOBJ.poster,
});
_movie.save(function (err, movie) {
console.log('查询错误,错误如下: ' + err);
});
res.redirect('/movie/' + movie._id);
};
});
// 修改
app.get('/admin/update/:id',function (req,res) {
if (id) {
Movie.findById(id, function (err, movie) {
if (err) {
console.log('修改查询错误,错误如下: ' + err);
}
_movie = _.extend(movie, movieOBJ);
_movie.save(function (err, movie) {
console.log('修改查询错误,错误如下: ' + err);
});
res.redirect('/movie/' + movie._id);
});
}
});
// 列表
app.get('/admin/list', function (req, res) {
Movie.fetch(function (err, movies) {
if (err) {
console.log("数据获取错误,错误=" + err);
}
res.render('list', {
title: '列表页',
movies: movies,
});
});
});