json-server数据模拟

比mockJS更简单的数据模拟服务

一、全局安装json-server服务

npm install -g json-server

二、 创建一个db.json 文件

{
  "posts": [
    {
      "id": 1,
      "title": "json-server",
      "author": "typicode"
    }
  ]
}

三、运行 json-server --watch db.json --port 3002

浏览器打开localhost:3002/posts 可以看到输出的json数据。

json-server把db.json 根节点的每一个key,当作了一个router。我们可以根据这个规则来编写测试数据。要注意的是,这个key不能包含符号 /

取数据

我们可以使用GET, POST, PUT, PATCH or DELETE 方法来对数据进行操作。

浏览器打开即可访问
– 获取所有:http://localhost:3004/posts
– 条件查找:http://localhost:3004/posts?title=橘子&author=大米
– 分页查找:http://localhost:3004/posts?_page=2&_limit=5
– 排序:http://localhost:3004/posts?_sort=price&_order=desc
– 取局部数据 Slice: http://localhost:3004/posts?_start=2&_end=4
– 模糊匹配:http://localhost:3004/posts?title_like=果
– 全文搜索:http://localhost:3004/posts?q=3

更多参考:
http://ww.qdxiaochuan.com/?id=565
https://gitcode.net/mirrors/typicode/json-server

你可能感兴趣的:(json)