Nodejs电影建站开发实例(下)

作为一个真正的网站,不能没有数据的支持,下面使用的数据库为mongodb,电影可能有的数据:电影名称、导演、国家、语言、上映时间、图片、简介、视频

4、使用路由

app.js

var express = require("express");
var app=express();
var path = require('path');
var index =require('./routes/index');
var admin = require('./routes/admin');


//设置模板引擎
app.set("view engine",'jade');
app.set('views','./views/pages');

//设置静态资源
app.use(express.static(path.join(__dirname, './public')));


app.use("/",index);
app.use("/admin",admin);



app.listen(3000,function(){
    console.log("请访问http://localhost:3000");
});

创建路由,把app.js的内容分别转移到routes下的index.js、admin.js

admin.js

var express = require('express');
var router = express.Router();

//localhost:3000/admin/add
router.get("/add",function(req,res){
    res.render('control.jade',{
        title:'后台电影添加页',
        movie:{
            title:'',
            director:'',
            country:'', 
            language:'',
            year:'',
            poster:'',
            summary:'',
            flash:''
        }
    });
});


//localhost:3000/admin/list
router.get("/list",function(req,res){
    res.render('list.jade',{
        title:'后台电影列表',
      movies:[
        {
            _id:1,
            title:'海绵宝宝3D',
            director:'保罗·蒂比特'
        }
      ]
    });
});

module.exports = router;
View Code

index.js

var express = require('express');
var router = express.Router();

//首页localhost:3000/
router.get('/', function(req, res, next) {
   res.render('index.jade',{
        title:'网站首页',
        movies:[
            {
            _id:1,
            title:"海绵宝宝3D",
            poster: 'http://img31.mtime.cn/mg/2015/11/17/094620.70277104_170X256X4.jpg'
            },
             {
            _id:2,
            title:"星际迷航3",
            poster:'http://img31.mtime.cn/mg/2016/09/01/143653.31713698_170X256X4.jpg'
            },
            {
            _id:3,
            title:"惊天绑架团",
            poster:'http://img31.mtime.cn/mg/2016/07/12/091819.79722823_170X256X4.jpg'
            },
            {
            _id:4,
            title:"爱宠大机密",
            poster:'http://img31.mtime.cn/mg/2016/06/21/093149.12209704_170X256X4.jpg'
            },
             {_id:5,
            title:"冰川时代4",
            poster:'http://img31.mtime.cn/mt/2012/07/19/131845.38602455_170X256X4.jpg'
            }
        ]
    });
});


//详情页
//localhost:3000/movie/1
router.get("/movie/:id",function(req,res){
    res.render('detail.jade',{
        title:'电影详情',
        movie:{
        title:'海绵宝宝3D',
        director:'保罗·蒂比特',
        country:'美国', 
        language:'英语',
        year:2016,
        poster:'http://img31.mtime.cn/mg/2015/11/17/094620.70277104_170X256X4.jpg',
        summary:'安东尼奥·班德拉斯饰演的大反派海盗杰克现身,他找到了一本神奇的宝书,可是想要获得完全的力量,一定要找到书中的最后一页。经过调查,这宝贵的最后一页,正存在海绵宝宝的家里。',
        flash:'#'
        }
    })
});

module.exports = router;
View Code

 

5.数据库连接

新增config文件夹用于存储数据库配置,在下面创建config.js存放数据库配置信息,mongoose.js用于连接数据库

初次使用mongoose?>>>>>>>>>>>>>>>>>

config.js

module.exports={
    mongodb:"mongodb://localhost:27017/test"
}

mongoose.js

var mongoose = require("mongoose");
var config = require("./config.js");


module.exports=function(){
    var db = mongoose.connect(config.mongodb);
    db.connection.on("error",function(err){
        console.log("数据库链接失败");
    });
    db.connection.on("open",function(err){
        console.log("数据库链接成功");
    });
};

app.js

....
//数据库连接
var mongoose =require("./config/mongoose.js");
var db=mongoose();
.....

 

 6、实现增删查看改

不知道怎么用mongodb增删查改?>>>>>>>>>>>>>>>>>>>

创建models文件夹,下面创建movie.model.js

var mongoose =require("mongoose");

var movieSchema = mongoose.Schema({
    //电影名称、导演、国家、语言、上映时间、图片、简介
    title:String,
    director:String,
    country:String, 
    language:String,
    year:Number,
    poster:String,
    summary:String,
    flash:String   
});

mongoose.model("movie",movieSchema);

 

在路由引入model

var mongoose = require('mongoose');
var movieModel = mongoose.model('movie');

因为添加页面和编辑页是共用的,为了便于判断,在添加页增加了id

 input(type="hidden",name="movie[_id]",value="#{movie._id}")

处理页:

//提交处理页:localhost:3000/admin/movie/do
router.post("/movie/do",function(req,res,next){
    //添加和编辑用的是同一个处理页面
    var movieObj = req.body.movie;
    //判断id是否已经存在,已存在则更新数据,否则添加新数据
    if(movieObj._id!=="undefined"){
       //存在id,更新
       movieModel.findOne({"_id":movieObj._id},function(err,doc){
            doc.title=movieObj.title;
            doc.director = movieObj.director;
            doc.country = movieObj.country;
            doc.language = movieObj.language;
            doc.year = movieObj.year;
            doc.poster = movieObj.poster;
            doc.summary = movieObj.summary;
            doc.flash = movieObj.flash;
            doc.save();
       })

    }else{
        //没有id,新增电影
   var movieEntry = new movieModel({
    title:movieObj.title,
    director:movieObj.director,
    country:movieObj.country, 
    language:movieObj.language,
    year:movieObj.year,
    poster:'http://img31.mtime.cn/mg/2016/06/21/093149.12209704_170X256X4.jpg',
    summary:movieObj.summary,
    flash:'http://v.youku.com/v_show/id_XODc4NDY0MjA4.html'
    });
        movieEntry.save();    
    }

    res.redirect('/admin/list');
});
View Code

删除:/admin/delete?id=..

router.get("/delete",function(req,res,next){
    var id=req.query.id;
    movieModel.findOne({"_id":id},function(err,doc){
        if(err){
            console.log('err:',err);
            return;
        }
        if(doc){
            doc.remove();
        }

    });
    res.redirect('/admin/list');
});
View Code

修改:

//admin/update/1
router.get("/update/:id",function(req,res){
    var id=req.params.id;
    movieModel.findOne({"_id":id},function(err,movie){
        if(err){
            console.log(err);
            return;
        }
        res.render("control.jade",{
            title:"编辑该电影",
            movie:movie
        });
    });

});
View Code

添加:

//localhost:3000/admin/add
router.get("/add",function(req,res){
    res.render('control.jade',{
        title:'后台电影添加页',
        movie:{
            title:'电影1',
            director:'史提芬',
            country:'美国', 
            language:'英语',
            year:'2016',
            poster:'http://img31.mtime.cn/mg/2015/03/27/120537.13212993_270X405X4.jpg',
            summary:'填写详情',
            flash:'#'
        }
    });
});
View Code

查询:

//首页localhost:3000/
router.get('/', function(req, res, next) {
    movieModel.find({},function(err,movies){
        if(err){
            console.log(err);
            return;
        }
        res.render('index.jade',{
               title:'网站首页',
               movies:movies 
        });
    });
});
View Code

查询单条:

//详情页
//localhost:3000/movie/1
router.get("/movie/:id",function(req,res){
    var id=req.params.id;
    movieModel.findOne({"_id":id},function(err,movie){
        if(err){
            console.log(err);
            return;
        }
        res.render("detail.jade",{
            title:"电影详情",
            movie:movie
        });
    });

});
View Code

 

 

终极测试:

 先清除所有表数据

添加一条

添加后列表:

编辑

编辑后列表

删除后列表

 

 下载>>

posted @ 2016-09-02 13:34  tinyphp  Views(4004)  Comments(0Edit  收藏  举报