json-server笔记
简介
- 使用json文件作为数据库,生成假的API
- id是默认主键,不传自增
- 修改数据时,put方式只传一个参数会将数据库变为一个参数,patch方式只更新传的那一个参数
固定规则
GET
:查询数据POST
:新增数据DELETE
:删除数据PUT
和PATCH
:修改数据
命令
- 安装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"
}
]
}
命令
- 监听文件:
json-server --watch db.json
- 显示帮助信息:
json-server -h
- 显示版本号:
json-server -v
- 禁止输出日志消息:
json-server -q
- 设置外键后缀(如post_id中的_id):
json-server --fks
- 设置数据的id属性:
json-server -i
- 设置反馈延时(ms):
json-server -d
- 设置快照目录:
json-server -S
- 禁止GZIP:
json-server --ng
- 禁用跨源资源共享:
json-server --nc
- 只读:
json-server --ro
- 设置静态文件:
json-server db.json -s ./目录名
- 指定中间件:
json-server -m
- 指定路由文件:
json-server db.json -r routes.json
- 主机地址:
json-server -H 0.0.0.0 json文件
- 端口号:
json-server -p6666 json文件
- 指定配置文件:
json-server -c
简单操作
- 增:
http://localhost:3000/posts
,参数写在body中- 删:
http://localhost:3000/posts/2
,删除id=2的数据,参数是主键的值- 改:
http://localhost:3000/posts/2
,修改id=2的参数,类似post传参- 查:
http://localhost:3000/posts
常用操作
- id查询:
1. 查询id为1的数据:http://localhost:3000/posts/1
- 条件查询:
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=张三
- 分页查询:
1._page
:页码,_limit
:每页的数据量,除了需要的数据,还会在header中返回总数,第一个,前一个,下一个,最后一个
1. 获取第二页数据每页三条:http://localhost:3000/comments?_page=2&_limit=3
- 排序查询:
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
- 切片查询:
1._start
:开始下标,_end
:结束下标,_limit
:片段长度,总数会放在header里
1. 返回下标从2-6的数据:http://localhost:3000/comments?_start=2&_end=6
或http://localhost:3000/comments?_start=2&_limit=4
- 范围查询:
1._gte
:大于等于,_lte
:小于等于,_ne
:不等于
1. id大于等于4的数据:http://localhost:3000/comments?id_gte=4
- 模糊查询:
1. 关键字:_like
1. comments接口body字段包含1的数据:http://localhost:3000/comments?body_like=1
- 全文查询:
1. 关键字:q
1. 查询所有字段中包含2的数据:http://localhost:3000/comments?q=2
- 外键关联查询:
1. 查询 posts 里 id 为 1 的所有 comments 内容:http://localhost:3000/posts/1/comments
- 关系拼装:
1._embed
:包含子资源 ,_expand
:包含父资源,关系拼装可以把主外键关联的2个接口拼接起来并返回
1. 包含子资源:http://localhost:3000/posts?_embed=comments
1. 在 comments 里,把 posts 里 id 为 2 的数据找出来并拼接起来:http://localhost:3000/posts/2?_embed=comments
- 配置路由:
1. 需求:模拟接口http://localhost:3000/api/users/1
2. 先创建routes.json:{"/api/*":"/$1"}
3. 再启动服务(指定路由文件)
4. 访问http://localhost:3000/api/posts
静态资源
- 默认配置:在根目录下创建
public
文件夹,里面放入html等文件- 自定义配置:
json-server db.json --static ./目录名
- 静态目录或public中可以放html,css,js,图片,视频等资源
- 查询public中的图片:
http://localhost:3000/图片名(要后缀)
其他
生成动态数据
根目录下创建db.js文件
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`
查询整个数据库:
http://localhost:3000/db
远程模式:
json-server 远程地址