nodeJS web端包使用 bower 管理

使用 npm 管理 server 端(服务端)的包
使用 bower 管理 web 端(客户端)的包

bower 的使用:
1.使用 npm 安装 bower
npm install bower -g

2.查看 bower 是否安装成功:
bower -v

3.搭建 bower 初始化环境
bower init
会生成一个bower.json 文件,与 package.json文件差不多

4.创建 bower 运行时文件
null>.bowerrc
并添加如下配置:
{“directory”: “./components”}
这里的路径是可以自由指定的,但必须使用双引号

5.使用 bower 安装需要使用的前端包,比如:bootstrap jquery angular
bower install bootstrap jquery angular --save

源代码的 GitHub路径如下:
https://github.com/slhuang520/study/tree/master/web/nodeJS/3part

.bowerrc

{
  "directory" : "./components"
}

bower.json

{
  "name": "view",
  "authors": [
    "slhuang520 <349733050@qq.com>"
  ],
  "description": "",
  "main": "",
  "license": "MIT",
  "homepage": "",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "dependencies": {
    "bootstrap": "^4.3.1",
    "jquery": "^3.4.1",
    "angular": "^1.7.8"
  }
}

package.json

{
  "name": "3part",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.16.4"
  }
}

app.js

server端

/**
 * 使用 npm 管理 server 端(服务端)的包
 * 使用 bower 管理 web 端(客户端)的包
 * @type {*|createApplication}
 */

var express = require("express");
var path = require("path");
var bodyParser = require("body-parser");

//创建server网站
var app = express();
// 获取Post body 传参
app.use(bodyParser.json());
// 如果有url加密的,需要解密
app.use(bodyParser.urlencoded({extended: false}));

//指定默认的访问页面(index.html)
app.use(express.static('view'));//路径指向 相对的文件夹路径

// Get 返回字符串
app.get("/a", function (req, res) {
    res.send("server response");
});
// Get 返回 Json
app.get("/b", function (req, res) {
    res.json({name: "Jim", age: 12});
})
// Post 返回 Json
app.post("/b", function (req, res) {
    res.json({name: "Jim", age: 12});
});
// All 返回 Json,all 可以接收 get 或是 Post
app.all("/c", function (req, res) {
    res.json({name: "Jim", age: 12});
});
// Get 通过路由传参
app.get("/d/:id", function (req, res) {
    var id = req.params.id;
    console.log(id);
    res.json({name: id, age: 12});
});
// Post body 传参
app.post("/d", function (req, res){
    var name = req.body.name;
    console.log(name);
    res.json({name: name, age: 12});
});

//通过路由显示相应的页面
app.use("/list", function (req, res) {
    res.status(200).sendFile(path.join(__dirname, "view", "list.html"));//返回200表示正常
});

app.use("/info", function (req, res) {
    res.sendFile(path.join(__dirname, "view", "info.html"))
});

//[*]表示所有,如果以上的路径都没有匹配时,则表明没有找到相应的页面
app.use("*", function (req, res) {// 这里的[*]是可以省略的
    res.sendFile(path.join(__dirname, "view", "err", "404.html"));
});

app.listen(3000, /*"192.168.56.1",*/ function (err) {
    if (err) {
        console.log("监听失败");
        throw err;
    }

    console.log("server 已经开启,默认IP: 127.0.0.1, Port:3000");
});

index.html

web 端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
    <link rel="stylesheet" href="components/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
index
<div id="msg"></div>
<button class="btn btn-danger" id="getText">GetText</button>
<button class="btn btn-danger" id="getJson">GetJson</button>
<button class="btn btn-danger" id="postJson">PostJson</button>
<button class="btn btn-danger" id="getAll">GetAll</button>
<button class="btn btn-danger" id="postAll">PostAll</button>
<button class="btn btn-danger" id="getByParam">Get路由传参</button>
<button class="btn btn-danger" id="postByParam">Post Body传参</button>

<script src="components/jquery/dist/jquery.min.js"></script>
<script>
    $("#getText").click(function (e) {
        $.get("/a", function (res) {
            console.log(res);
            $("#msg").text(res);
        });
    });
    $("#getJson").click(function (e) {
        $.get("/b", function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
    $("#postJson").click(function (e) {
        $.post("/b", function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
    $("#getAll").click(function (e) {
        $.get("/c", function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
    $("#postAll").click(function (e) {
        $.post("/c", function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
    $("#getByParam").click(function (e) {
        $.get("/d/Jim", function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
    $("#postByParam").click(function (e) {
        $.post("/d", {name: "Jim"}, function (res) {
            console.log(res);
            $("#msg").text(res.name + " : " + res.age);
        });
    });
</script>
</body>
</html>

页面效果:
在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值