美文网首页
使用json-server快速搭建本地数据接口

使用json-server快速搭建本地数据接口

作者: 吕保鑫 | 来源:发表于2020-03-05 13:00 被阅读0次

    官方网站:https://github.com/typicode/json-server

    终端输入:sudo npm install -g json-server
    
    创建一个json文件
    {
      "user": [
        {
          "id": 1,
          "name": "james",
          "phone": "333-444-555",
          "email": "james@sina.com",
          "age": 30,
          "companyid": 1
        },
        {
          "id": 2,
          "name": "curry",
          "phone": "333-444-555",
          "email": "curry@sina.com",
          "age": 31,
          "companyid": 2
        },
        {
          "id": 3,
          "name": "davis",
          "phone": "333-444-555",
          "email": "davis@sina.com",
          "age": 3,
          "companyid": 3
        },
        {
          "id": 4,
          "name": "jackson",
          "phone": "333-444-555",
          "email": "jackson@sina.com",
          "age": 33,
          "companyid": 3
        },
        {
          "name": "bryant",
          "phone": "333-444-555",
          "email": "bryant@sina.com",
          "age": 41,
          "companyid": 4,
          "id": 5
        }
      ],
      "company": [
        {
          "id": 1,
          "companyname": "apple",
          "des": "apple is good"
        },
        {
          "id": 2,
          "companyname": "huawei",
          "des": "huawei is good"
        },
        {
          "id": 3,
          "companyname": "oppo",
          "des": "oppo is good"
        },
        {
          "id": 4,
          "companyname": "alibaba",
          "des": "alibaba is good"
        }
      ]
    }
    
    get请求
    # 起动json-server 
    json-server --watch db.json
    # 获取所有用户
    http://localhost:3000/user
    # 获取用户id为1的
    http://localhost:3000/user/1
    # 获取所有公司
    http://localhost:3000/company/
    # 获取公司id为1的
    http://localhost:3000/company/1
    # 根据公司名字获取公司
    http://localhost:3000/company?companyname=apple
    # 根据多个公司名字获取
    http://localhost:3000/company?companyname=apple&companyname=alibaba
    # 获取一个页面显示两条信息
    http://localhost:3000/company?_page=1&_limit=2
    # 升序排序
    http://localhost:3000/company?_sort=id&_order=asc
    # 降序排序
    http://localhost:3000/company?_sort=id&_order=desc
    # 获取年龄大于30的
    http://localhost:3000/user?age_gte=30
    # 获取年龄区间的
    http://localhost:3000/user?age_gte=3&age_lte=31
    # 搜索姓名
    http://localhost:3000/user?q=cu
    
    post,patch,delete,需要下载postman,官方网站:[https://www.postman.com/downloads/](https://www.postman.com/downloads/),设置Headers,key是Content-Type,value是application/json,Body选择raw
    {"name": "bryant", "phone": "333-444-555","email":"bryant@sina.com","age":41,"companyid":4}
    

    相关文章

      网友评论

          本文标题:使用json-server快速搭建本地数据接口

          本文链接:https://www.haomeiwen.com/subject/lcqxrhtx.html