• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Node.js和Ajax阶段作业项目----博学谷主页

武飞扬头像
好累好像摆
帮助1

目标

利用html css知识对页面进行布局,微课部分利用artTemplate模板进行渲染,数据端口用nodejs写,用express创建服务器,并托管我们写好的页面

准备工作:

1.按照效果图对网页进行布局分析

学新通

 2.设计HTML CSS布局

 学新通

3.使用Express搭建静态服务器

1、使用npm init 初始化项目
2、安装相关依赖包(比如:express、jQuery、artTemplate)
3、新建在项目中新建相关目录和文件(存放html和css的目录、
启动文件)
4、 编写Express代码,启动服务器,展示html页面
4.启动后端服务,提供前端所需数据接口
1、使用npm install 安装依赖
2、通过nodemon app.js 启动后端服务
学新通

样本数据 (我们需要把样本json数据存到数据库中去)

  1.  
    {
  2.  
    "result": {
  3.  
    "data": [
  4.  
    { "id": "5d246c4b0b00e74af45f6093", "name": "【前端】微信小程序", "count": "20", "price": "199", "img": "http://127.0.0.1:3001/images/wx.jpg" },
  5.  
    { "id": "5d246c4b0b00e74af45f6094", "name": "【前端】快应用", "count": "100", "price": "19", "img": "http://127.0.0.1:3001/images/quickAPP.jpg" },
  6.  
    { "id": "5d246c4b0b00e74af45f6095", "name": "【前端】JavaScript", "count": "100", "price": "免费", "img": "http://127.0.0.1:3001/images/js.jpg" },
  7.  
    { "id": "5d246c4b0b00e74af45f6096", "name": "【前端】就业班", "count": "300", "price": "14980", "img": "http://127.0.0.1:3001/images/FE.png" },
  8.  
    { "id": "5d246c583823d14b08268220", "name": "【前端】微信小程序", "count": "20", "price": "199", "img": "http://127.0.0.1:3001/images/wx.jpg" },
  9.  
    { "id": "5d246c583823d14b08268221", "name": "【前端】快应用", "count": "100", "price": "19", "img": "http://127.0.0.1:3001/images/quickAPP.jpg" },
  10.  
    { "id": "5d246c583823d14b08268222", "name": "【前端】JavaScript", "count": "100", "price": "免费", "img": "http://127.0.0.1:3001/images/js.jpg" },
  11.  
    { "id": "5d246c583823d14b08268223", "name": "【前端】就业班", "count": "300", "price": "14980", "img": "http://127.0.0.1:3001/images/FE.png" },
  12.  
    { "id": "5d246c972b4aa94b4db6c5bd", "name": "【前端】微信小程序", "count": "20", "price": "199", "img": "http://127.0.0.1:3001/images/wx.jpg" },
  13.  
    { "id": "5d246c972b4aa94b4db6c5be", "name": "【前端】快应用", "count": "100", "price": "19", "img": "http://127.0.0.1:3001/images/quickAPP.jpg" },
  14.  
    { "id": "5d246c972b4aa94b4db6c5bf", "name": "【前端】JavaScript", "count": "100", "price": "免费", "img": "http://127.0.0.1:3001/images/js.jpg" },
  15.  
    { "id": "5d246c972b4aa94b4db6c5c0", "name": "【前端】就业班", "count": "300", "price": "14980", "img": "http://127.0.0.1:3001/images/FE.png" },
  16.  
    { "id": "5d246caec502604b6c1f52e8", "name": "【前端】微信小程序", "count": "20", "price": "199", "img": "http://127.0.0.1:3001/images/wx.jpg" },
  17.  
    { "id": "5d246caec502604b6c1f52e9", "name": "【前端】快应用", "count": "100", "price": "19", "img": "http://127.0.0.1:3001/images/quickAPP.jpg" },
  18.  
    { "id": "5d246caec502604b6c1f52ea", "name": "【前端】JavaScript", "count": "100", "price": "免费", "img": "http://127.0.0.1:3001/images/js.jpg" },
  19.  
    { "id": "5d246caec502604b6c1f52eb", "name": "【前端】就业班", "count": "300", "price": "14980", "img": "http://127.0.0.1:3001/images/FE.png" },
  20.  
    { "id": "5d246cdd64d3e54ba11c8923", "name": "【前端】微信小程序", "count": "20", "price": "199", "img": "http://127.0.0.1:3001/images/wx.jpg" },
  21.  
    { "id": "5d246cdd64d3e54ba11c8924", "name": "【前端】快应用", "count": "100", "price": "19", "img": "http://127.0.0.1:3001/images/quickAPP.jpg" },
  22.  
    { "id": "5d246cdd64d3e54ba11c8925", "name": "【前端】JavaScript", "count": "100", "price": "免费", "img": "http://127.0.0.1:3001/images/js.jpg" },
  23.  
    { "id": "5d246cdd64d3e54ba11c8926", "name": "【前端】就业班", "count": "300", "price": "14980", "img": "http://127.0.0.1:3001/images/FE.png" }
  24.  
     
  25.  
    ]
  26.  
    }
  27.  
    }
学新通

———————————————————————————————————————————

代码部分:

WEB部分代码

HTML

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title>Document</title>
  8.  
    <link rel="stylesheet" href="./css/normalize.css">
  9.  
    <link rel="stylesheet" href="./css/index.css">
  10.  
    <script src="./js/jquery.js"></script>
  11.  
    <script src="./js/template-web.js"></script>
  12.  
    <script src="./js/index.js"></script>
  13.  
    </head>
  14.  
    <body>
  15.  
    <div class="header">
  16.  
    <div class="header-item">
  17.  
    <img src="./img/email.png" alt="" style="height: 40px;">
  18.  
    </div>
  19.  
    <div class="header-item">
  20.  
    <img src="./img/headerLogo.png" alt="" style="height: 40px;">
  21.  
    </div>
  22.  
    <div class="header-item">
  23.  
    <img src="./img/fdj.png" alt="">
  24.  
    </div>
  25.  
    </div>
  26.  
    <div class="body-one">
  27.  
    <img src="./img/FE.png" alt="" style="height: 170px;width: 100%;">
  28.  
    </div>
  29.  
    <div class="body-two">
  30.  
    <div class="body-two-header">
  31.  
    ·就业课·
  32.  
    </div>
  33.  
    <div class="body-two-main">
  34.  
    <div class="body-two-main-item">
  35.  
    <img src="./img/fe-list.png" alt="">
  36.  
    </div>
  37.  
    <div class="body-two-main-item">
  38.  
    <img src="./img/java.png" alt="">
  39.  
    </div>
  40.  
    <div class="body-two-main-item">
  41.  
    <img src="./img/data.png" alt="">
  42.  
    </div>
  43.  
    </div>
  44.  
    <div class="body-two-main2">
  45.  
    <div class="body-two-main-item">
  46.  
    <img src="./img/python.png" alt="">
  47.  
    </div>
  48.  
    <div class="body-two-main-item">
  49.  
    <img src="./img//ui.png" alt="">
  50.  
    </div>
  51.  
    <div class="body-two-main-item">
  52.  
    <img src="./img/fe-list.png" alt="">
  53.  
    </div>
  54.  
    </div>
  55.  
    </div>
  56.  
    <div class="body-three">
  57.  
    <div class="body-three-header">
  58.  
    ·微课·
  59.  
    </div>
  60.  
    <div class="body-three-item">
  61.  
    <img src="./img/wx.jpg" alt="">
  62.  
    <div class="body-three-item-header">【前端】微信小程序</div>
  63.  
    <div>
  64.  
    <span class="body-three-item-study">20人学习</span>
  65.  
    <span class="body-three-item-price">¥199</span>
  66.  
    </div>
  67.  
    </div>
  68.  
     
  69.  
    <div class="body-three-item">
  70.  
    <img src="./img/wx.jpg" alt="">
  71.  
    <div class="body-three-item-header">【前端】微信小程序</div>
  72.  
    <div>
  73.  
    <span class="body-three-item-study">20人学习</span>
  74.  
    <span class="body-three-item-price">¥199</span>
  75.  
    </div>
  76.  
    </div>
  77.  
     
  78.  
     
  79.  
     
  80.  
    </div>
  81.  
    <div class="body-foot">
  82.  
    <div class="body-foot-index">
  83.  
    <img src="./img/icon-index.png" alt="">
  84.  
    <span>首页</span>
  85.  
    </div>
  86.  
    <div class="body-foot-fenlei">
  87.  
    <img src="./img/icon-fenlei.png" alt="">
  88.  
    <span>分类</span>
  89.  
    </div>
  90.  
    </div>
  91.  
     
  92.  
    <!-- 此处为模板引擎渲染所需要的模板 -->
  93.  
    <script type="text/html" id="muban">
  94.  
    {{each results}}
  95.  
    <div class="body-three-item">
  96.  
    <img src="{{$value.img}}" alt="">
  97.  
    <div class="body-three-item-header">{{$value.name}}</div>
  98.  
    <div>
  99.  
    <span class="body-three-item-study">{{$value.count}}人学习</span>
  100.  
    <span class="body-three-item-price">{{$value.price}}</span>
  101.  
    </div>
  102.  
    </div>
  103.  
    {{/each}}
  104.  
    </script>
  105.  
    </body>
  106.  
    </html>
学新通

css:

  1.  
    body{
  2.  
    background-color: #f1f1F1;
  3.  
    max-width:540px;
  4.  
    min-width: 320px;
  5.  
    margin: 0 auto;
  6.  
    }
  7.  
    .header{
  8.  
    background: #446ADF;
  9.  
    height: 64px;
  10.  
    width: 100%;
  11.  
    line-height: 64px;
  12.  
    display:flex;
  13.  
    flex-direction: row;
  14.  
    justify-content: space-around;
  15.  
    position: fixed;
  16.  
    top: 0px;
  17.  
    clear: both;
  18.  
    margin: 0;
  19.  
    z-index:99999;
  20.  
    max-width:540px;
  21.  
    min-width: 320px;
  22.  
    }
  23.  
    .header-item{
  24.  
    margin: auto;
  25.  
    position: relative;
  26.  
    top: 10px;
  27.  
    }
  28.  
    .body-one{
  29.  
    max-width:540px;
  30.  
    min-width: 320px;
  31.  
    width: 100%;
  32.  
    height: 170px;
  33.  
    position: absolute;
  34.  
    top: 64px;
  35.  
    }
  36.  
    .body-two{
  37.  
    width: 100%;
  38.  
    height: 242px;
  39.  
    max-width:540px;
  40.  
    min-width: 320px;
  41.  
    background-color: #FFFFFF;
  42.  
    position: absolute;
  43.  
    top: 250px; /*170 64 16*/
  44.  
    }
  45.  
    .body-two-header{
  46.  
    height: 52px;
  47.  
    font-size: 18px;
  48.  
    color: #446ADF;
  49.  
    display: flex;
  50.  
    line-height: 38px;
  51.  
    justify-content: center;
  52.  
    letter-spacing: 10px;
  53.  
    /* background-color: pink; */
  54.  
    }
  55.  
    .body-two-main{
  56.  
    width: 100%;
  57.  
    height: 95px;
  58.  
    /* background: gray; */
  59.  
    display:flex;
  60.  
    flex-direction: row;
  61.  
    justify-content: space-around;
  62.  
    }
  63.  
    .body-two-main2{
  64.  
    width: 100%;
  65.  
    height: 95px;
  66.  
    display:flex;
  67.  
    flex-direction: row;
  68.  
    justify-content: space-around;
  69.  
    }
  70.  
     
  71.  
    .body-two-main-item{
  72.  
    float: left;
  73.  
    }
  74.  
    .body-two-main-item img{
  75.  
    height: 70px;
  76.  
    width: 70px;
  77.  
    border-radius: 100%;
  78.  
    }
  79.  
     
  80.  
    .body-three{
  81.  
    max-width:540px;
  82.  
    min-width: 320px;
  83.  
    width: 100%;
  84.  
    height: auto;
  85.  
    /* background:gray; */
  86.  
    position: absolute;
  87.  
    top: 508px;
  88.  
    background-color: #FFFFFF;
  89.  
    }
  90.  
     
  91.  
    .body-three-header{
  92.  
    height: 54px;
  93.  
    font-size: 18px;
  94.  
    color: #446ADF;
  95.  
    display: flex;
  96.  
    line-height: 38px;
  97.  
    justify-content: center;
  98.  
    letter-spacing: 10px;
  99.  
    /* background-color: pink; */
  100.  
    }
  101.  
     
  102.  
    .body-three-item{
  103.  
    float: left;
  104.  
    /* background-color: lightgreen; */
  105.  
    height: 158px;
  106.  
    margin: 10px;
  107.  
    margin-top: 0;
  108.  
    margin-bottom: 0;
  109.  
    }
  110.  
     
  111.  
    .body-three-item img{
  112.  
    height: 100px;
  113.  
    width: 163.92px;
  114.  
    border-radius: 10%;
  115.  
    }
  116.  
     
  117.  
    .body-three-item-header{
  118.  
    align-items: center;
  119.  
    font-size: 16px;
  120.  
    font-weight: 800;
  121.  
    padding-bottom: 6px;
  122.  
    }
  123.  
    .body-three-item-study{
  124.  
    float: left;
  125.  
    font-size: 14px;
  126.  
    color: #D0D0D0;
  127.  
    }
  128.  
    .body-three-item-price{
  129.  
    float: right;
  130.  
    font-size: 14px;
  131.  
    color: #446ADF;
  132.  
    }
  133.  
     
  134.  
    .body-foot{
  135.  
    max-width:540px;
  136.  
    min-width: 320px;
  137.  
    width: 100%;
  138.  
    height: 58px;
  139.  
    position: fixed;
  140.  
    bottom: 0;
  141.  
    background: #FFFFFF;
  142.  
    z-index:99999;
  143.  
    }
  144.  
    .body-foot-index{
  145.  
    width: 40px;
  146.  
    float: left;
  147.  
    margin-left: 40px;
  148.  
    }
  149.  
    .body-foot-index img{
  150.  
    height: 30px;
  151.  
    }
  152.  
    .body-foot-index span{
  153.  
    color: #1296db;
  154.  
    }
  155.  
    .body-foot-fenlei{
  156.  
    width: 40px;
  157.  
    float: right;
  158.  
    margin-right: 40px;
  159.  
    }
  160.  
    .body-foot-fenlei img{
  161.  
    height: 30px;
  162.  
    }
  163.  
    .body-foot-fenlei span{
  164.  
    color: #1296db;
  165.  
    }
学新通

js:(js部分很少就是ajax请求服务和模板引擎使用的一些操作)

  1.  
    $.get('http://127.0.0.1:3001/class',(res)=>{
  2.  
    var strHtml = template('muban',res)
  3.  
    $('.body-three').html(strHtml)
  4.  
    })

———————————————————————————————服务器端部分代码

服务器端入口app.js

  1.  
    const express = require('express')
  2.  
    const app = express()
  3.  
    const cors = require('cors')
  4.  
    app.use(cors())
  5.  
    //创建模板渲染数据存放路由
  6.  
    const classRouter = require('./router/class')
  7.  
    app.use('/class',classRouter)
  8.  
    //创建图片存放路由
  9.  
    const imgRouter = require('./router/image')
  10.  
    app.use('/images',imgRouter)
  11.  
    //创建主页托管路由
  12.  
    const indexRouter = require('./router/index')
  13.  
    app.use('/',indexRouter)
  14.  
    app.listen(3001,()=>{
  15.  
    console.log('express server running at http://127.0.0.1:3001')
  16.  
    })
学新通

数据存放路由

  1.  
    const express = require('express')
  2.  
    const router = express.Router()
  3.  
    const indexRouterHandler = require('../router_handler/index')
  4.  
    router.use('/',indexRouterHandler.showindex)
  5.  
    module.exports = router

数据存放路由处理端

  1.  
    const db = require('../db/index')
  2.  
    exports.getNews = (req,res)=>{
  3.  
    const sql=`select * from cours`
  4.  
    db.query(sql,(err,results)=>{
  5.  
    if(err) return res.send(err)
  6.  
    return res.send({
  7.  
    results:results,
  8.  
    status:1,
  9.  
    })
  10.  
    })
  11.  
    }

注意此处应用了数据库所以在此前我们应先建立数据库与我们服务器的联系

建立db文件夹 db部分代码

  1.  
    const mysql = require('mysql')
  2.  
     
  3.  
    const db = mysql.createPool({
  4.  
    host:'127.0.0.1',
  5.  
    port:'3308',
  6.  
    user:'root',
  7.  
    password:'admin123',
  8.  
    database:'db_bxg'
  9.  
    })
  10.  
    //此处数据根据你们自己的数据库来决定
  11.  
    module.exports=db

图片存放路由

  1.  
    const express = require('express')
  2.  
    const router = express.Router()
  3.  
    const imageRouter= require('../router_handler/image')
  4.  
    router.use('/',imageRouter.getImg)
  5.  
    module.exports = router

图片存放路由处理端

  1.  
    const fs = require('fs')
  2.  
    const path = require('path')
  3.  
    exports.getImg=readImg = (req,res)=>{
  4.  
    const url = req.url
  5.  
    let fpath = ''
  6.  
    fpath = path.join(__dirname,'../','/image',url)
  7.  
    fs.readFile(fpath,'binary',(err,data)=>{
  8.  
    if(err){
  9.  
    return res.end()
  10.  
    }
  11.  
    res.write(data,"binary");
  12.  
    res.end()
  13.  
    })
  14.  
    }

最后是托管我们写好的web部分

index路由端

  1.  
    const express = require('express')
  2.  
    const router = express.Router()
  3.  
    const indexRouterHandler = require('../router_handler/index')
  4.  
    router.use('/',indexRouterHandler.showindex)
  5.  
    module.exports = router

index路由处理

  1.  
    const fs = require('fs')
  2.  
    const path = require('path')
  3.  
    exports.showindex=(req,res)=>{
  4.  
    const url = req.url
  5.  
    console.log(url)
  6.  
    let fpath = ''
  7.  
    if(url==='/'){
  8.  
    fpath = path.join(__dirname,'../','/constroler/index.html')
  9.  
    }else{
  10.  
    fpath = path.join(__dirname,'../','/constroler',url)
  11.  
    }
  12.  
    fs.readFile(fpath,'binary',(err,data)=>{
  13.  
    if(err){
  14.  
    return res.end('404 Not Found')
  15.  
    }
  16.  
    return res.end(data,"binary")
  17.  
    })
  18.  
    }
学新通

这篇好文章是转载于:编程之路

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 编程之路
  • 本文地址: /boutique/detail/tanhhfeebh
系列文章
更多 icon
同类精品
更多 icon
继续加载