简介
JSON-server 是运行在 Node.JS 环境一个模块, 可以单独运行. JSON-server 读取本地 JSON 文件, 并根据 JSON 文件提供一个完整的后台服务, 包括增删改查 (分页, 模糊, 全文检索).
使用场景
简单的小程序
前端开发人员模拟数据用的后台接口
安装
NPM i JSON-server -g
使用
创建 db.JSON
- {
- "users":[
- {"id":1,"name":"zhangsan","password":"123"}
- ],
- "produces":[
- {"id":1,"name":"苹果","price":3.13},
- {"id":2,"name":"梨子","price":4}
- ]
- }
运行 cmd
JSON-server --watch db.JSON
61f58392-d145-444d-92c2-ce054b31aa53.PNG
访问 users 接口, 返回数组或单个对象
http://localhost:3000/users
411b2fe7-c732-45d5-9163-40aae57cd744.PNG
http://localhost:3000/users/1
2091775e-d3e5-44e0-835f-6064b95d8827.PNG
常用 API
- # 查询全部数据
- get /users
- # 查询单条数据
- get /users/1
- # 增加数据
- post /users {
- ...
- }
- # 修改数据
- put /users/1 {
- ...
- }
- # 条件查询 (查询 name 有张三, id>10 的数据)
get /users?name_like = 张三 & id_lt=10
- # 分页查询 (查询第 3 个, 每页显示 20 条)
- get /users?_page=3&_limit=20
- # 全文检索
get /users?q = 张三
- # 排序 (按照 id 倒叙)
- get /users?_sort=id&_order=desc
参考资料
npmjs 官网文档
来源: http://www.jianshu.com/p/85918c401f1b