44
55在不需要书写代码的情况下,获取一个完整的假数据 REAST API 只需要 30 秒(我是认真的)。
66
7+ 如果同时使用 [ Postman] ( http://getpostman.com/ ) 一款功能强大的网页调试与模拟发送网页 HTTP 请求的 Chrome 插件,则真真的能实现前后端完全分离。
8+
79## 基础环境:
810
911- [ Node.js] ( https://nodejs.org )
1012- npm
13+ - Chrome 插件 Postman
1114
1215### 查看环境:
1316
@@ -18,8 +21,60 @@ $ npm -v
18212.1.18
1922```
2023
21- ## 实例
24+ ## ` JSON-server ` 实例
25+
22261.创建` db.json ` 文件
27+ ``` json
28+ {
29+ "posts" : [
30+ {
31+ "id" : 1 ,
32+ "title" : " json-server" ,
33+ "author" : " typicode"
34+ },
35+ {
36+ "id" : 2 ,
37+ "title" : " json-server" ,
38+ "author" : " typicode2"
39+ },
40+ {
41+ "id" : 3 ,
42+ "title" : " json-server" ,
43+ "author" : " typicode3"
44+ },
45+ {
46+ "id" : 4 ,
47+ "title" : " fy-server" ,
48+ "author" : " typicode3"
49+ },
50+ {
51+ "id" : 5 ,
52+ "title" : " fy-server" ,
53+ "author" : " typicode3"
54+ }
55+ ],
56+ "comments" : [
57+ {
58+ "id" : 1 ,
59+ "body" : " some comment" ,
60+ "postId" : 1
61+ },
62+ {
63+ "id" : 2 ,
64+ "body" : " some comment" ,
65+ "postId" : 1
66+ },
67+ {
68+ "id" : 3 ,
69+ "body" : " some comment" ,
70+ "postId" : 2
71+ }
72+ ],
73+ "profile" : {
74+ "name" : " typicode"
75+ }
76+ }
77+ ```
2378
24792.安装` json-server `
2580
@@ -35,11 +90,11 @@ json-server --watch db.json
3590
36914.访问数据
3792
38- http://localhost:3000/
93+ [ http://localhost:3000/ ] ( http://localhost:3000/ )
3994
40- ## 路由
95+ ### JSON-server 路由
4196
42- ### 过滤器(Filter)
97+ #### 过滤器(Filter)
4398
4499使用` . ` 访问深层数据
45100
@@ -49,34 +104,34 @@ GET /posts?id=1&id=2
49104GET /comments?author.name=typicode
50105```
51106
52- ### 数据分页(Slice) ` _start ` ` _end ` ` _limit `
107+ #### 数据分页(Slice) ` _start ` ` _end ` ` _limit `
53108
54109```
55110GET /posts?_start=20&_end=30
56111GET /posts/1/comments?_start=20&_end=30
57112GET /posts/1/comments?_start=20&_limit=10
58113```
59114
60- ### 分类(Sort) ` _sort ` ` _order ` (DESC ASC)
115+ #### 分类(Sort) ` _sort ` ` _order ` (DESC ASC)
61116
62117```
63118GET /posts?_sort=views&_order=DESC
64119GET /posts/1/comments?_sort=votes&_order=ASC
65120```
66121
67- ### 范围(Range) ` _gte ` ` _lte `
122+ #### 范围(Range) ` _gte ` ` _lte `
68123
69124```
70125GET /posts?views_gte=10&views_lte=20
71126```
72127
73- ### 搜索(Full-text search) ` q `
128+ #### 搜索(Full-text search) ` q `
74129
75130```
76131GET /posts?q=internet
77132```
78133
79- ### 关系数据(Relationships) ` _embed ` ` _expand ` ` / `
134+ #### 关系数据(Relationships) ` _embed ` ` _expand ` ` / `
80135
81136` _embed ` 包含子类数据
82137` _expand ` 包含父类数据
@@ -95,9 +150,13 @@ GET /comments/1?_expand=post
95150GET /posts/1/comments
96151```
97152
98- ### 获取所有假数据
153+ #### 获取所有假数据
99154
100155```
101156GET /db
102157```
103- [ lowdb] ( https://github.com/typicode/lowdb )
158+
159+
160+ ### JSON-server 路由配置
161+
162+
0 commit comments