-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
README.md
209 lines (150 loc) · 4.65 KB
/
README.md
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# json-server
[![Node.js CI](https://github.com/typicode/json-server/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/json-server/actions/workflows/node.js.yml)
> [!IMPORTANT]
> Viewing beta v1 documentation – usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0)
👋 _Hey! Using React, Vue or Astro? Check my new project [MistCSS](https://github.com/typicode/mistcss) to write 50% less code._
## Install
```shell
npm install json-server
```
## Usage
Create a `db.json` or `db.json5` file
```json
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
],
"profile": {
"name": "typicode"
}
}
```
<details>
<summary>View db.json5 example</summary>
```json5
{
posts: [
{ id: '1', title: 'a title', views: 100 },
{ id: '2', title: 'another title', views: 200 },
],
comments: [
{ id: '1', text: 'a comment about post 1', postId: '1' },
{ id: '2', text: 'another comment about post 1', postId: '1' },
],
profile: {
name: 'typicode',
},
}
```
You can read more about JSON5 format [here](https://github.com/json5/json5).
</details>
Pass it to JSON Server CLI
```shell
$ npx json-server db.json
```
Get a REST API
```shell
$ curl http://localhost:3000/posts/1
{
"id": "1",
"title": "a title"
}
```
Run `json-server --help` for a list of options
## Sponsors ✨
| Sponsors |
| :---: |
| <a href="https://mockend.com/" target="_blank"><img src="https://jsonplaceholder.typicode.com/mockend.svg" height="100px"></a> |
| Sponsors |
| :---: |
| <a href="https://konghq.com/"><img src="https://github.com/typicode/json-server/assets/5502029/e8d8ecb2-3c45-4f60-92d0-a060b820fa7f" height="75px"></a> |
| Sponsors | |
| :---: | :---: |
| <a href="https://www.storyblok.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/c6b10674-4ada-4616-91b8-59d30046b45a" height="35px"></a> | <a href="https://betterstack.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/44679f8f-9671-470d-b77e-26d90b90cbdc" height="35px"></a> |
| <a href="https://www.globalsoftwarecompanies.com" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/d0bb2e9b-9b74-4d91-9c9e-f3c87e152918" height="35px"></a> | <a href="https://beeceptor.com/?utm_source=json-server"><img src="https://github.com/typicode/json-server/assets/5502029/57b852a6-60b9-426b-986e-a148e82783c2" height="35px"></a> |
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
## Sponsorware
> [!NOTE]
> This project uses the [Fair Source License](https://fair.io/). Only organizations with 3+ users are kindly asked to contribute a small amount through sponsorship [sponsor](https://github.com/sponsors/typicode) for usage. __This license helps keep the project sustainable and healthy, benefiting everyone.__
>
> For more information, FAQs, and the rationale behind this, visit [https://fair.io/](https://fair.io/).
## Routes
Based on the example `db.json`, you'll get the following routes:
```
GET /posts
GET /posts/:id
POST /posts
PUT /posts/:id
PATCH /posts/:id
DELETE /posts/:id
# Same for comments
```
```
GET /profile
PUT /profile
PATCH /profile
```
## Params
### Conditions
- ` ` → `==`
- `lt` → `<`
- `lte` → `<=`
- `gt` → `>`
- `gte` → `>=`
- `ne` → `!=`
```
GET /posts?views_gt=9000
```
### Range
- `start`
- `end`
- `limit`
```
GET /posts?_start=10&_end=20
GET /posts?_start=10&_limit=10
```
### Paginate
- `page`
- `per_page` (default = 10)
```
GET /posts?_page=1&_per_page=25
```
### Sort
- `_sort=f1,f2`
```
GET /posts?_sort=id,-views
```
### Nested and array fields
- `x.y.z...`
- `x.y.z[i]...`
```
GET /foo?a.b=bar
GET /foo?x.y_lt=100
GET /foo?arr[0]=bar
```
### Embed
```
GET /posts?_embed=comments
GET /comments?_embed=post
```
## Delete
```
DELETE /posts/1
DELETE /posts/1?_dependent=comments
```
## Serving static files
If you create a `./public` directory, JSON Serve will serve its content in addition to the REST API.
You can also add custom directories using `-s/--static` option.
```sh
json-server -s ./static
json-server -s ./static -s ./node_modules
```
## Notable differences with v0.17
- `id` is always a string and will be generated for you if missing
- use `_per_page` with `_page` instead of `_limit`for pagination
- use Chrome's `Network tab > throtling` to delay requests instead of `--delay` CLI option