在不需要书写代码的情况下,获取一个完整的假数据 REAST API 只需要 30 秒(我是认真的)。
如果同时使用 Postman 一款功能强大的网页调试与模拟发送网页 HTTP 请求的 Chrome 插件,则真真的能实现前后端完全分离。
- Node.js
- npm
- Chrome 插件 Postman
$ node -v
v0.10.35
$ npm -v
2.1.18
1.创建db.json
文件
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
},
{
"id": 2,
"title": "json-server",
"author": "typicode2"
},
{
"id": 3,
"title": "json-server",
"author": "typicode3"
},
{
"id": 4,
"title": "fy-server",
"author": "typicode3"
},
{
"id": 5,
"title": "fy-server",
"author": "typicode3"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
},
{
"id": 2,
"body": "some comment",
"postId": 1
},
{
"id": 3,
"body": "some comment",
"postId": 2
}
],
"profile": {
"name": "typicode"
}
}
2.安装json-server
$ npm install -g json-server
3.启动json-server
json-server --watch db.json
4.访问数据
使用.
访问深层数据
GET /posts?title=json-server&author=typicode
GET /posts?id=1&id=2
GET /comments?author.name=typicode
GET /posts?_start=20&_end=30
GET /posts/1/comments?_start=20&_end=30
GET /posts/1/comments?_start=20&_limit=10
GET /posts?_sort=views&_order=DESC
GET /posts/1/comments?_sort=votes&_order=ASC
GET /posts?views_gte=10&views_lte=20
GET /posts?q=internet
_embed
包含子类数据
_expand
包含父类数据
GET /posts?_embed=comments
GET /posts/1?_embed=comments
GET /comments?_expand=post
GET /comments/1?_expand=post
GET /posts/1/comments
GET /db