//app.js
var express = require('express')
var path = require('path')
var mongoose = require('mongoose')
var _ = require('underscore')
var bodyParser = require('body-parser');
var Movie = require('./models/movie')
var port = process.env.PORT || 3000
var app = express();
mongoose.connect('mongodb://localhost/imooc')
app.set('views',__dirname+'/views/pages')
app.set('view engine','jade')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'res')));
app.listen(port)
console.log('imooc started on port '+ port)
//index page
app.get('/',(req,res)=>{
Movie.fetch(function(err,movies){
if(err){
console.log(err)
}
res.render('index',{
title:'imooc 首页',
movies:movies
})
})
})
// schemas\movie.js
var mongoose = require('mongoose')
var MovieSchema = require('../schemas/movie')
var Movie = mongoose.model('Movie',MovieSchema)
module.exports = Movie
//models\movies.js
var mongoose = require('mongoose')
var MovieSchema = new mongoose.Schema({
doctor: String,
title: String,
language: String,
country: String,
summary: String,
flash: String,
poster: String,
year: Number,
meta:{
createAt:{
type:Date,
default:Date.now()
},
updataAt:{
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()
})
MovieSchema.static = {
fetch: function (cb) {
return this
.find({})
.sort('meta.updateAt')
.exec(cb)
},
findById: function (id,cb) {
return this
.findOne({_id: id})
.exec(cb)
}
}
module.exports = MovieSchemaTypeError: Movie.fetch is not a function
1 回答
- 1 回答
- 0 关注
- 2389 浏览
添加回答
举报
0/150
提交
取消