源码讲解 node+mongodb 建站攻略(一期)第二节

源码讲解 node+mongodb 建站攻略(一期)第二节

上一节,我们完成了模拟数据,这次我们来玩儿真正的数据库,mongodb。

代码

首先给大家看看目录结构。

QQ截图20150810140831

大家可以比对一下,跟第一节我们加了那些内容。

1,我们新建文件夹schemas,新建文件movie.js,主要提供对数据模型,实例化,对数据的增删改查方法。

var mongoose=require(\'mongoose\'); var MovieSchema=new mongoose.Schema({ doctor:String, title:String, language: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(); }); MovieSchema.statics={ fetch:function(cb){ return this .find({}) .sort(\'meta.updateAt\') .exec(cb); }, findById:function(id,cb){ return this .findOne({_id:id}) .exec(cb); } }; module.exports=MovieSchema;

2,我们新建文件夹schemas,新建文件movie.js ,定义变量

var mongoose=require(\'mongoose\'); var MovieSchema=require(\'../schemas/movie\'); var Movie=mongoose.model(\'Movie\',MovieSchema); module.exports=Movie;

3,准备工作做好了,开始页面上来操作数据了,还是从入口文件开始

var express = require(\'express\'); var path = require(\'path\'); var bodyParser = require(\'body-parser\'); var mongoose = require(\'mongoose\'); var _ = require(\'underscore\');//提供新的支持underscore var Movie = require("./models/movie"); var port = process.PORT || 3000; var app = express(); mongoose.connect("mongodb://localhost/demo1"); app.set(\'views\', \'./views/pages\'); app.set(\'view engine\', \'jade\'); // app.use(express.static(path.join(__dirname, \'public\'))); app.use(express.static(path.join(__dirname,\'bower_components\'))); app.use(bodyParser.urlencoded({ extended: true })); app.locals.moment = require(\'moment\'); app.listen(port); console.log(\'demo1 started on port \' + port); //index page app.get(\'/\', function(req, res) { //查询所有数据,调用的是models-->movie.js里面的方法 Movie.fetch(function(err, movies) { if (err) { console.log(err); } //将查询的数据返回到index.jade页面 res.render(\'index\', { title: \'demo1 首页\', movies: movies }); }); }); //detail page app.get(\'/movie/:id\', function(req, res) { //详情页面,获取request里面的id,根据id查询一条数据 var id = req.params.id; Movie.findById(id, function(err, movie) { res.render(\'detail\', { title: \'demo1\' + movie.title, id: id, movie: movie }); }) }); //admin page app.get(\'/admin/movie\', function(req, res) { res.render(\'admin\', { title: \'demo1 后台录入页\', movie: { _id: \'\', doctor: \'\', country: \'\', title: \'\', year: \'\', poster: \'\', language: \'\', flash: \'\', summary: \'\' } }); }); //admin update movie app.get(\'/admin/update/:id\', function(req, res) { //获取request里面的id,根据id判断是修改 var id = req.params.id; if (id) { Movie.findById(id, function(err, movie) { res.render(\'admin\', { title: \'demo1 后台更新页\', movie: movie }); }); } }); //admin delete movie app.delete(\'/admin/list\',function(req,res){ //根据id删除一条数据 var id = req.query.id; if(id){ Movie.remove({_id:id},function(err,movie){ if(err){ console.log(err); }else{ res.json({success:1}); } }); } }) //admin post movie app.post(\'/admin/movie/new\', function(req, res) { console.log(req.body); console.log(req.body.movie); //获取数据对象,保存数据,新增。 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, language: movieObj.language, country: movieObj.country, year: movieObj.year, poster: movieObj.poster, flash: movieObj.flash, summary: movieObj.summary }); _movie.save(function(err, movie) { if (err) { console.log(err); } 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: \'demo1 列表页\', movies: movies }); }); });

3,删除数据写的是button,所以要对button写一个事件,在处理删除。

在bower_components下面新建js文件夹,然后新建admin.js。同时在list.jade下面引入 script(src="/js/admin.js"),注意缩进。

QQ截图20150810143443

$(function(){ $(\'.del\').click( function(e){ var target = $(e.target); var id = target.data(\'id\'); var tr = $(\'.item-id-\'+ id); $.ajax({ type:\'DELETE\', url:\'/admin/list?id=\'+id }) .done(function(reaults){ if(reaults.success===1){ if(tr.length>0){ tr.remove(); } } }) } ) })

最后直接在完整图片预览一下吧

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwggfj.html