zoukankan      html  css  js  c++  java
  • json-server 的基本使用

    官方链接:https://www.npmjs.com/package/json-server#add-custom-routes

    简述

    可以根据json文件快速在本地启动服务提供API接口

    安装

    npm install -g json-server
    

    基本使用

    1. 创建db.json文件
    {
      "students": [
        {
          "id": 1,
          "name": "孙悟空",
          "age": 20
        },
        {
          "id": 2,
          "name": "猪八戒",
          "age": 30
        },
        {
          "id": 5,
          "name": "沙和尚",
          "age": 16
        },
        {
          "id":4,
          "name":"白骨精",
          "age":18
        },
        {
          "id":3,
          "name":"捣乱的",
          "age":16
        }
      ],
      "address": [
        "北京路1号",
        "上海路2号",
        "广州路3号"
      ]
    }
    
    1. 启动json-server服务
    json-server --watch db.json
    
    1. 发送请求
      获取students中的所有数据
    http://localhost:3000/students
    

    获取指定数据

    获取students中id为3的数据

    http://localhost:3000/students/3
    

    获取students中name为白骨精并且age为18的数据

    http://localhost:3000/students?name=白骨精&age=18
    

    分页

    • 使用_limit来限制默认返回的条数,默认会返回所有;
    • 使用_page指定页数
      需求1:返回students中2条数据
    http://localhost:3000/students?_limit=2
    

    需求2:返回students中第二页的数据

    http://localhost:3000/students?_limit=2&_page=2
    

    排序

    • 默认情况下根据升序排列;
    • 使用_sort指定什么进行排序
    • 使用_order指定是升序(asc)还是降序(desc)。
      需求:根据students中的age进行降序
    http://localhost:3000/students?_order=desc&_sort=age
    

    需求:根据students中age降序、id的升序

    http://localhost:3000/students?_order=desc,asc&_sort=age,id
    

    获取数组中指定范围的数据

    类似数组中的Array.slice

    • 使用_start指定数组的开始索引,包括此索引
    • 使用_end指定数组的结束索引,不包括此索引
      需求:获取students中索引为1到4(不包含4)的数据
    http://localhost:3000/students?_start=1&_end=4
    

    需求:获取students中索引为1,长度为2的数据

    http://localhost:3000/students?_start=1&_limit=2
    

    比较

    • 大于使用xx_gte greate than equal
    • 小于使用xx_lte less than equal
    • 不等于使用xx_ne not equal
    • 正则匹配使用xx_like
      需求:获取students中age大于等于18的数据
    http://localhost:3000/students?age_gte=18
    

    需求:获取students中name包含了"和"的数据

    http://localhost:3000/students?name_like=和
    

    全文检索

    • 大概的意思是只要有包含就匹配把
      需求:获取students中age包含16的数据
    http://localhost:3000/students?q=16
    
  • 相关阅读:
    NGINX反向代理与负载均衡
    kubernetes介绍
    Linux下yum出现no module named pycurl 解决办法
    MySQL中间件介绍
    Memcached做Tomcat的session共享
    MySQL高负载优化
    Centos下安装Tomcat7
    浅谈世界坐标系,相机坐标系,图像坐标系,像素坐标系的关系
    相机标定方法之初探
    ubuntu18.04安装kalibr相机标定工具
  • 原文地址:https://www.cnblogs.com/it774274680/p/15036505.html
Copyright © 2011-2022 走看看