json-server

1.安装json-server

json-server

npm install -g json-server  全局安装
npm install json-server     局部安装

2.创建数据

//创建db.json存放json数据
{
    "users": [
        {
            "id": 1,
            "username": "张三",
            "password": "zs123"
        },
        {
            "id": 2,
            "username": "李四",
            "password": "ls123"
        },
        {
            "id": 3,
            "username": "王五",
            "password": "ww123"
        }
    ]
}

局部安装json-server需使用npm init -y 命令生成package.json

  "scripts": {
    "serve": "json-server db.json"
    //"serve": "json-server --watch db.json"   //监听
  },

3.启动json服务器

npm run serve                 //局部
json-server db.json           //全局
json-server --watch db.json   //全局-监听
json-server --watch db.json --port 3004//全局-监听-端口

路由

GET    /users
GET    /users/1
POST   /users
PUT    /users/1
DELETE /users/1

使用例子

<body h-screen m-0 p-0 flex justify-center items-center>
  <button border-hidden p-4 b-rd-2 bg-blue c-white>获取全部用户</button>
  <button border-hidden p-4 b-rd-2 bg-purple c-white>获取id=3的用户</button>
  <button border-hidden p-4 b-rd-2 bg-pink c-white>添加赵六</button>
  <button border-hidden p-4 b-rd-2 bg-orange c-white>修改id=4</button>
  <button border-hidden p-4 b-rd-2 bg-red c-white>删除id=4</button>
  <script src="https://cdn.jsdelivr.net/npm/@unocss/runtime/attributify.global.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script>
    const btn = document.getElementsByTagName("button");
    btn[0].onclick = () => {
      axios.get("http://localhost:3000/users").then((res) => {
        if (res.status == 200) {
          console.log(res.data);
        }
      }).catch((err) => { console.log(err) });
    }
    btn[1].onclick = () => {
      axios.get("http://localhost:3000/users/3").then((res) => {
        if (res.status == 200) {
          console.log(res.data);
        }
      }).catch((err) => { console.log(err) });
    }
    btn[2].onclick = () => {
      axios.post("http://localhost:3000/users", {
        "username": "赵六",
        "password": "zl123"
      }).then((res) => {
        if (res.status == 200) {
          console.log("添加成功");
        }
      }).catch((err) => { console.log(err) });
    }
    btn[3].onclick = () => {
      axios.put("http://localhost:3000/users/4", {
        "username": "赵六啊",
        "password": "zl666"
      }).then((res) => {
        if (res.status == 200) {
          console.log("修改成功");
        }
      }).catch((err) => { console.log(err) });
    }
    btn[4].onclick = () => {
      axios.delete("http://localhost:3000/users/4").then((res) => {
        if (res.status == 200) {
          console.log("删除成功");
        }
      }).catch((err) => { console.log(err) });
    }
  </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小钱要努力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值