官网
https://www.npmjs.com/package/json-server#getting-started
1 全局安装json-server
npm install -g json-server
2 创建json文件存储数据
// db.json
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
3 启动json-server服务器
json-server --watch db.json
4 Restfull风格接口
- GET获取
- POST插入
- PUT更新整条数据
- PATCH部分更新
- DELETE 删除数据
- _page 页码
- _limit 每页显示数量,默认10
GET /posts
GET /posts/1
POST /posts
PUT /posts/1
PATCH /posts/1
DELETE /posts/1
GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2
GET /comments?author.name=typicode
GET /posts?_page=7
GET /posts?_page=7&_limit=20