-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-server-guide.txt
97 lines (78 loc) · 2.95 KB
/
json-server-guide.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//1. GET
- Get whole database
http://localhost:3000/meetingrooms/db
- Get all
http://localhost:3000/meetingrooms/employees
- Filter some posts
http://localhost:3000/meetingrooms?id=1 //return 1 item
http://localhost:3000/meetingrooms/employees?first=Nickolas&first=Rupert //return 2 items
- Search (q)
http://localhost:3000/meetingrooms/employees?q=senior
//return all fields that has 'senior'
- Sorting (_sort & _order)
Default order: ASC
http://localhost:3000/meetingrooms/employees?_sort=name&_order=ASC
*Note: Custom routes sorting is not returning right sequence
- Relationship: Get related records from the request
//To include children resources, add _embed
// parent resource, add _expand
http://localhost:3000/meetingrooms/departments?name=:department&_embed=employees
//2. POST (create)
http://localhost:3000/meetingrooms/employees/all
with body:
{
"first": "Wonder",
"last": "Girl",
"phone": "239.933.1719",
"email": "[email protected]",
"level": "mid",
"age": 27,
"departmentId": 2
}
*Note:
- A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data
- Does not support multiple entries
//3. PUT (update)
http://localhost:3000/meetingrooms/employees/:id
with body:
{
"first": "Wonder",
"last": "Girl",
"phone": "239.933.1719",
"email": "[email protected]",
"level": "senior",
}
*Note:
- A POST, PUT or PATCH request should include a Content-Type: application/json header to use the JSON in the request body. Otherwise it will result in a 200 OK but without changes being made to the data
- Does not support multiple entries
- Need to provide full entry
//4. DELETE
http://localhost:3000/meetingrooms/employees/8
*Note: Does not support multiple entries
//5. Custom routes (Rewriter)
{
//To set endpoint for meeting rooms
//Accessed all by Http://localhost:3000/meetingrooms/
"/meetingrooms/": "/",
"/meetingroomsDB/": "/db",
"/employees/all": "/employees/",
"/employeesLevel=:level": "/employees?level=:level",
"/employeesBelow30": "/employees?age_lte=31",
}
========== FAQ 🙋 ============
//What I do if I started a new project?
1. Copy sample folder - meetingRooms
2. In server.js, add:
- var newProjectRouter = require("./<newProjectFolder>/<newProjectRoute>");
- server.use(newProjectRouter)
3. In <newProjectRewriter.json>, change:
- "/newprojectendpoint/": "/"
- configure the rest of the APIs URL
//If I want to test in my machine, how?
1. Have the code in your machine
2. node server.js
//What is this json-server?
💁 https://github.com/typicode/json-server
It's better to have knowledge in :
- Node.js
- Express: https://expressjs.com/en/4x/api.html