nodejs app get 提交两次请求
第一次在这贴代码,话说上次排版有点乱,现在已经改过来了~~谢谢大家提醒!!
var express = require("express")
var path = require('path')
var mongoose = require('mongoose')
var _ = require('underscore')
var Movie = require('./models/movie')
var port = process.env.PORT || 3000
var app = express()
var bodyParser = require('body-parser');
mongoose.connect('mongodb://localhost:27017/imooc_projects')
app.set('views', './views/pages')
app.set('view engine', 'jade')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'bower_components')))
app.listen(port)
console.log('imooc started on port ' + port);
//index page
app.get('/', function (req, res) {
Movie.fetch(function(err, movies) {
if (err){
console.log(err)
}
res.render('index', {
title: 'imooc 首页',
movies: movies
})
})
})
//detail page
app.get('/movie/:id', function (req, res) {
var id = req.params.id
console.log("putain")
console.log(id)
Movie.findById(id, function(err, movie) {
console.log(movie)
res.render('detail', {
title: 'imooc' + movie.title,
movie: movie
})
})
})
//admin page
app.get('/admin/movie', function (req, res) {
res.render('admin', {
title: 'imooc 后台录入页',
movie: {
director: '',
country: '',
title: '',
year: '',
poster: '',
language: '',
flash: '',
summary: ''
}
})
})
// admin update movie
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
})
})
}
})
//admin post movie
app.post('/admin/movie/new', function(req, res) {
var id = req.body.movie._id
var movieObj = req.body.movie
var _movie
console.log(id)
if (id !== undefined) {
Movie.findById(id, function(err, movie) {
if (err) {
console.log(err)
}
console.log("alkfoaj1")
_movie = _.extend(movie, movieObj)
//console.log(movie)
//console.log(movieObj)
//console.log(_movie)
//console.log("alkfoaj2")
_movie.save(function(err, movie) {
if (err) {
console.log(err)
}
//console.log("alkfoaj3")
res.redirect('/movie/' + movie._id)
})
})
}
else {
console.log("else")
_movie = new Movie({
director: movieObj.director,
title: movieObj.title,
country: movieObj.country,
language: movieObj.language,
year: movieObj.year,
poster: movieObj.poster,
summary: movieObj.summary,
flash: movieObj.flash
})
_movie.save(function(err, movie) {
if (err) {
console.log(err)
}
//console.log(movie._id + "a")
res.redirect('/movie/' + movie._id)
})
}
})
//list page
app.get('/admin/list', function (req, res) {
Movie.fetch(function(err, movies) {
if (err){
console.log(err)
}
res.render('list', {
title: 'imooc 列表页',
movies: movies
})
})
})
\n```
```js\n
var mongoose =require ('mongoose')
var MovieSchema = new mongoose.Schema({
director: String,
title: String,
language: String,
country: String,
summary: String,
flash: String,
poster: String,
year: Number,
meta:{
createAt:{
type: Date,
default: Date.now()
},
updateAt:{
type: Date,
default: Date.now()
}
}
})
MovieSchema.pre('save',function(next) {
if(this.isNew){
this.meta.createAt = this.meta.updateAt = Date.now();
}
else{
this.meta.updateAt = Date.now()
}
next()
//console.log("aa")
})
MovieSchema.statics = {
fetch: function(cb){
return this
.find({})
.sort('meta.updateAt')
.exec(cb)
},
findById: function(id, cb){
//console.log("aaa")
return this
.findOne({_id: id})
.exec(cb)
}
}
module.exports = MovieSchema
\n```
```js\n
extend ../layout
block content
.container
.row
form.form-horizontal(method="post",action="/admin/movie/new")
//-input(type="hidden", name="movie[_id]", value="#{movie._id}")
.form-group
label.col-sm-2.control-label(for="inputTitle") 电影名字
.col-sm-10
input#inputTitle.col-sm-10.form-control(type="text",name="movie[title]",value="#{movie.title}")
.form-group
label.col-sm-2.control-label(for="inputDirector") 电影导演
.col-sm-10
input#inputDirector.col-sm-10.form-control(type="text",name="movie[director]",value="#{movie.director}")
.form-group
label.col-sm-2.control-label(for="inputCountry") 国家
.col-sm-10
input#inputCountry.col-sm-10.form-control(type="text",name="movie[country]",value="#{movie.country}")
.form-group
label.col-sm-2.control-label(for="inputLanguage") 语种
.col-sm-10
input#inputLanguage.col-sm-10.form-control(type="text",name="movie[language]",value="#{movie.language}")
.form-group
label.col-sm-2.control-label(for="inputPoster") 海报地址
.col-sm-10
input#inputPoster.col-sm-10.form-control(type="text",name="movie[poster]",value="#{movie.poster}")
.form-group
label.col-sm-2.control-label(for="inputFlash") 片源地址
.col-sm-10
input#inputFlash.col-sm-10.form-control(type="text",name="movie[flash]",value="#{movie.flash}")
.form-group
label.col-sm-2.control-label(for="inputYear") 上映年份
.col-sm-10
input#inputYear.col-sm-10.form-control(type="text",name="movie[year]",value="#{movie.year}")
.form-group
label.col-sm-2.control-label(for="inputSummary") 电影简介
.col-sm-10
input#inputSummary.col-sm-10.form-control(type="text",name="movie[summary]",value="#{movie.summary}")
.form-group
.col-sm-offset-2.col-sm-10
button.btn.btn-default(type="submit") 录入
报错:
也就是这段代码执行了两次: 经 @eqiuno 提醒,貌似没发现重复定义的路由~~到底哪里的问题?
11 回复
block content
.container
.row
.col-md-7
//-embed(src="#{movie.flash}",allowFullScreen="true",quality="high",width="720",height="600" align="middle",type="application/x-shockwave-flash")
video(src="#{movie.flash}",autoplay="true" width="720",height="600" )
.col-md-5
dl.dl-horizontal
dt 电影名字
dd=movie.title
dt 导演
dd=movie.director
dt 国家
dd=movie.country
dt 语言
dd=movie.language
dt 上映年份
dd=movie.year
dt 简介
dd=movie.summary
\n```
这句出的问题, 请求/movie/w
// src="/movie/w"
video(src="#{movie.flash}",autoplay="true" width="720",height="600" )
@helloedmund
…
流程是这样的:
你post一个新的movie ->
创建完成,跳到movie页,res.redirect('/movie/' + movie._id)
, 这是第一次请求 ->
在这里app.get('/movie/:id'
渲染展示详情 ->
浏览器显示这个详情, 解析video
标签, 自动请求src
, 也就是/movie/w
, 这是第二次