Nodejs電影建站開發執行個體(上)

來源:互聯網
上載者:User

標籤:

網站環境:使用express架構、bootstrap樣式、jade模板、mongoose資料庫

npm insatll express -gnpm insatll jada -gnpm insatll mongoose -g

1、建立express工程:myMovie

進入工程執行npm install,npm start 後 訪問很正常,可以往下繼續了

 

2、建立路由

初擬可能瀏覽的入口================

網站跟目錄:localhost:3000/

某個電影詳情頁:localhost:3000/movie/1

後台電影添加頁:localhost:3000/admin/add

後台列表頁:localhost:3000/admin/list

根據所需修改app.js

var express = require("express");var app=express();//訪問網站跟目錄:localhost:3000/app.get("/",function(req,res){    res.send("網站首頁");});//localhost:3000/movie/1app.get("/movie/:id",function(req,res){    res.send("電影詳情");});//localhost:3000/admin/addapp.get("/admin/add",function(req,res){    res.send("後台電影添加頁");});//localhost:3000/admin/listapp.get("/admin/list",function(req,res){    res.send("後台電影列表");});app.listen(3030,function(){    console.log("請訪問http://localhost:3030");});

 

3、jade準備視圖

index.jade首頁、add.jade後台添加頁、detail.jade(電影詳情)、list.jade(後台電影列表)的準備,為了便於觀察,放入基本的資訊,用於關聯jade視圖

app.js

var express = require("express");var app=express();var path = require(‘path‘);//設定模板引擎app.set("view engine",‘jade‘);app.set(‘views‘,‘./views/pages‘);//設定靜態資源app.use(express.static(path.join(__dirname, ‘./public‘)));//訪問網站跟目錄:localhost:3000/app.get("/",function(req,res){    res.render(‘index.jade‘,{        title:‘網站首頁‘,        movies:{}    });});//localhost:3000/movie/1app.get("/movie/:id",function(req,res){    res.render(‘detail.jade‘,{        title:‘電影詳情‘,        movie:{}    })});//localhost:3000/admin/addapp.get("/admin/add",function(req,res){    res.render(‘control.jade‘,{        title:‘後台電影添加頁‘,        movie:{}    });});//localhost:3000/admin/listapp.get("/admin/list",function(req,res){    res.render(‘list.jade‘,{        title:‘後台電影列表‘,      movies:{}    });});app.listen(3000,function(){    console.log("請訪問http://localhost:3000");});

layout.jade

doctype htmlhtml   head    meta(charset="utf-8")    title #{title}    include ./includes/link.jade  body    include ./includes/header.jade    block content

header.jade

.container    .row        .page-header            h1 #{title}

index.jade、control.jade、detail.jade、list.jade

extends ../layout.jadeblock content

 

 

4、jade資料深化

app.js

var express = require("express");var app=express();var path = require(‘path‘);//設定模板引擎app.set("view engine",‘jade‘);app.set(‘views‘,‘./views/pages‘);//設定靜態資源app.use(express.static(path.join(__dirname, ‘./public‘)));//訪問網站跟目錄:localhost:3000/app.get("/",function(req,res){    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/1app.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:‘#‘        }    })});//localhost:3000/admin/addapp.get("/admin/add",function(req,res){    res.render(‘control.jade‘,{        title:‘後台電影添加頁‘,        movie:{            title:‘‘,            director:‘‘,            country:‘‘,             language:‘‘,            year:‘‘,            poster:‘‘,            summary:‘‘,            flash:‘‘        }    });});//localhost:3000/admin/listapp.get("/admin/list",function(req,res){    res.render(‘list.jade‘,{        title:‘後台電影列表‘,      movies:[        {            _id:1,            title:‘海綿寶寶3D‘,            director:‘保羅·蒂位元‘        }      ]    });});app.listen(3000,function(){    console.log("請訪問http://localhost:3000");});
View Code

index.jade

extends ../layout.jadeblock content .container    .row        each item in movies           .col-md-2             .thumbnall                a(href="/movie/#{item._id}")                    img(src="#{item.poster}",alt="#{item.title}")                .caption                    h3 #{item.title}                    p: a.btn.btn-primary(href="/movie/#{item._id}") 查看詳情
View Code

detail.jade

extends ../layout.jadeblock content    .container        .row            .col-md-7                video(src="#{movie.flash}",autoplay="true",width="720",height="400")            .col-md-5                dl.dl-horizontal                    dt 電影名字                    dd=movie.title                    dt 導演                    dd=movie.director                    dt 國家                    dd=movie.country                    dt 發行年度                    dd=movie.year                    dt 簡介                    dd=movie.summary
View Code

list.jade

extends ../layout.jadeblock content    .container        .row            table.table.table-hover.table-bordered                thead                    tr                        th 電影名字                        th 導演                        th 查看                        th 更新                        th 刪除                tbody                    each item in movies                        tr                            td #{item.title}                            td #{item.director}                            td: a(target="_blank",href="../movie/#{item._id}") 查看                            td: a(target="_blank",href="../movie/update/#{item._id}") 修改                            td                                 a.btn.btn-danger.del(type="button",href="/admin/delete?id=#{item._id}") 刪除
View Code

control.jade

extends ../layout.jadeblock content    .container        .row            form.form-horizontal(method="post",action="/admin/movie/do")                //電影名字                .form-group                    label.col-sm-2.control-label(for="inputTitle") 電影名字:                    .col-sm-10                        input#inputTitle.form-control(type="text",name="movie[title]",value="#{movie.title}")                 //導演                .form-group                    label.col-sm-2.control-label(for="inputTitle") 導演:                    .col-sm-10                        input#inputDirector.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="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-2                    .col-sm-10                        input.btn.btn-default(type="submit",name="submit")
View Code

 各頁效果預覽

 下載>>

目前,靜態頁面已經準備完畢,接著將接入mongodb

Nodejs電影建站開發執行個體(上)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.