json-server笔记

json-server笔记

简介

  1. 使用json文件作为数据库,生成假的API
  2. id是默认主键,不传自增
  3. 修改数据时,put方式只传一个参数会将数据库变为一个参数,patch方式只更新传的那一个参数

固定规则

  1. GET:查询数据
  2. POST:新增数据
  3. DELETE:删除数据
  4. PUTPATCH:修改数据

命令

  1. 安装json-server:npm i -g json-server

json结构

{
  "posts":
  [
    {
      "id":1,
      "title":"json-server",
      "author":"typicode"
    }
  ],
  "comoents":
  [
    {
      "id":1,
      "body":"some comment",
      "postId":1
    }
  ],
  "profile":
  [
    {
      "name":"typicode"
    }
  ]
}

命令

  1. 监听文件:json-server --watch db.json
  2. 显示帮助信息:json-server -h
  3. 显示版本号:json-server -v
  4. 禁止输出日志消息:json-server -q
  5. 设置外键后缀(如post_id中的_id):json-server --fks
  6. 设置数据的id属性:json-server -i
  7. 设置反馈延时(ms):json-server -d
  8. 设置快照目录:json-server -S
  9. 禁止GZIP:json-server --ng
  10. 禁用跨源资源共享:json-server --nc
  11. 只读:json-server --ro
  12. 设置静态文件:json-server db.json -s ./目录名
  13. 指定中间件:json-server -m
  14. 指定路由文件:json-server db.json -r routes.json
  15. 主机地址:json-server -H 0.0.0.0 json文件
  16. 端口号:json-server -p6666 json文件
  17. 指定配置文件:json-server -c

简单操作

  1. 增:http://localhost:3000/posts,参数写在body中
  2. 删:http://localhost:3000/posts/2,删除id=2的数据,参数是主键的值
  3. 改:http://localhost:3000/posts/2,修改id=2的参数,类似post传参
  4. 查:http://localhost:3000/posts

常用操作

  1. id查询:
    1. 查询id为1的数据:http://localhost:3000/posts/1
  2. 条件查询:
    1. 单一条件查询:查找author为张三的所有数据:http://localhost:3000/posts?author=张三
    1. 多条件查询(且):author为张三且title为文章:http://localhost:3000/posts?author=张三&title=文章
    1. 多条件查询(或):title为22或title为33:http://localhost:3000/posts?title=22&title=33
    1. 深度属性查询:查询 authorInfo.name=张三的数据:http://localhost:3000/posts?authorinfo.name=张三
  3. 分页查询:
    1. _page:页码,_limit:每页的数据量,除了需要的数据,还会在header中返回总数,第一个,前一个,下一个,最后一个
    1. 获取第二页数据每页三条:http://localhost:3000/comments?_page=2&_limit=3
  4. 排序查询:
    1. _sort:排序的标记,_order:排序规则,asc:升序,desc:降序
    1. 以id为参考字段升序排列:http://localhost:3000/comments?_sort=id&_order=asc
    1. 先按postId升序排列,在对id倒序排列:http://localhost:3000/comments?_sort=postId,id&_order=asc,desc
  5. 切片查询:
    1. _start:开始下标,_end:结束下标,_limit:片段长度,总数会放在header里
    1. 返回下标从2-6的数据:http://localhost:3000/comments?_start=2&_end=6http://localhost:3000/comments?_start=2&_limit=4
  6. 范围查询:
    1. _gte:大于等于,_lte:小于等于,_ne:不等于
    1. id大于等于4的数据:http://localhost:3000/comments?id_gte=4
  7. 模糊查询:
    1. 关键字:_like
    1. comments接口body字段包含1的数据:http://localhost:3000/comments?body_like=1
  8. 全文查询:
    1. 关键字:q
    1. 查询所有字段中包含2的数据:http://localhost:3000/comments?q=2
  9. 外键关联查询:
    1. 查询 posts 里 id 为 1 的所有 comments 内容:http://localhost:3000/posts/1/comments
  10. 关系拼装:
    1. _embed:包含子资源 ,_expand:包含父资源,关系拼装可以把主外键关联的2个接口拼接起来并返回
    1. 包含子资源:http://localhost:3000/posts?_embed=comments
    1. 在 comments 里,把 posts 里 id 为 2 的数据找出来并拼接起来:http://localhost:3000/posts/2?_embed=comments
  11. 配置路由:
    1. 需求:模拟接口http://localhost:3000/api/users/1
    2. 先创建routes.json:{"/api/*":"/$1"}
    3. 再启动服务(指定路由文件)
    4. 访问http://localhost:3000/api/posts

静态资源

  1. 默认配置:在根目录下创建public文件夹,里面放入html等文件
  2. 自定义配置:json-server db.json --static ./目录名
  3. 静态目录或public中可以放html,css,js,图片,视频等资源
  4. 查询public中的图片:http://localhost:3000/图片名(要后缀)

其他

  1. 生成动态数据

    1. 根目录下创建db.js文件

    2. module.exports=()=>{
      //创建一百个user
        for(let i=0;i<100;i++){
          data.users.push({id:i,name:`user${i}`})
        }
        return data
      }
      
    1. `json-server db.js`
    
  2. 查询整个数据库:http://localhost:3000/db

  3. 远程模式:json-server 远程地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值