json-server 0.17.4 → 1.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +44 -20
- package/README.md +81 -555
- package/lib/app.d.ts +8 -0
- package/lib/app.js +92 -0
- package/lib/app.test.d.ts +1 -0
- package/lib/app.test.js +90 -0
- package/lib/bin.d.ts +2 -0
- package/lib/bin.js +109 -0
- package/lib/observer.d.ts +11 -0
- package/lib/observer.js +30 -0
- package/lib/service.d.ts +39 -0
- package/lib/service.js +281 -0
- package/lib/service.test.d.ts +1 -0
- package/lib/service.test.js +294 -0
- package/package.json +40 -89
- package/public/output.css +1164 -0
- package/lib/cli/bin.js +0 -5
- package/lib/cli/index.js +0 -77
- package/lib/cli/run.js +0 -213
- package/lib/cli/utils/is.js +0 -16
- package/lib/cli/utils/load.js +0 -69
- package/lib/server/body-parser.js +0 -9
- package/lib/server/defaults.js +0 -73
- package/lib/server/index.js +0 -10
- package/lib/server/mixins.js +0 -76
- package/lib/server/rewriter.js +0 -14
- package/lib/server/router/delay.js +0 -10
- package/lib/server/router/get-full-url.js +0 -10
- package/lib/server/router/index.js +0 -91
- package/lib/server/router/nested.js +0 -26
- package/lib/server/router/plural.js +0 -273
- package/lib/server/router/singular.js +0 -52
- package/lib/server/router/validate-data.js +0 -17
- package/lib/server/router/write.js +0 -8
- package/lib/server/utils.js +0 -26
- package/public/favicon.ico +0 -0
- package/public/index.html +0 -85
- package/public/script.js +0 -76
- package/public/style.css +0 -113
package/README.md
CHANGED
|
@@ -1,633 +1,159 @@
|
|
|
1
|
-
|
|
1
|
+
> [!IMPORTANT]
|
|
2
|
+
> Viewing alpha v1 documentation – usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0)
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
# json-server
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
[](https://github.com/typicode/json-server/actions/workflows/node.js.yml)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
* [JSONPlaceholder - Live running version](https://jsonplaceholder.typicode.com)
|
|
9
|
-
* [__My JSON Server__ - no installation required, use your own data](https://my-json-server.typicode.com)
|
|
8
|
+
## Install
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* :owl: [lowdb - local JSON database](https://github.com/typicode/lowdb)
|
|
14
|
-
* ✅ [xv - a beautifully simple and capable test runner](https://github.com/typicode/xv)
|
|
15
|
-
|
|
16
|
-
<p> </p>
|
|
17
|
-
|
|
18
|
-
<h2 align="center">Gold sponsors 🥇</h2>
|
|
19
|
-
|
|
20
|
-
<p> </p>
|
|
21
|
-
|
|
22
|
-
<p align="center">
|
|
23
|
-
<a href="https://tryretool.com/?utm_source=sponsor&utm_campaign=typicode" target="_blank">
|
|
24
|
-
<img src="https://i.imgur.com/IBItATn.png" height="70px">
|
|
25
|
-
</a>
|
|
26
|
-
</p>
|
|
27
|
-
|
|
28
|
-
<p> </p>
|
|
29
|
-
|
|
30
|
-
<p align="center">
|
|
31
|
-
<a href="https://mockend.com/" target="_blank">
|
|
32
|
-
<img src="https://jsonplaceholder.typicode.com/mockend.svg" height="70px">
|
|
33
|
-
</a>
|
|
34
|
-
</p>
|
|
35
|
-
|
|
36
|
-
<p> </p>
|
|
37
|
-
|
|
38
|
-
<p> </p>
|
|
39
|
-
|
|
40
|
-
<h2 align="center">Silver sponsors 🥈</h2>
|
|
41
|
-
|
|
42
|
-
<p> </p>
|
|
43
|
-
|
|
44
|
-
<p align="center">
|
|
45
|
-
<a href="https://cased.com" target="_blank">
|
|
46
|
-
<img src="https://user-images.githubusercontent.com/5502029/194441951-b7dca49d-efd6-496d-900b-288004717f11.png" height="55px">
|
|
47
|
-
</a>
|
|
48
|
-
</p>
|
|
49
|
-
|
|
50
|
-
<p> </p>
|
|
51
|
-
|
|
52
|
-
<p> </p>
|
|
53
|
-
|
|
54
|
-
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
|
|
55
|
-
|
|
56
|
-
## Sponsor
|
|
57
|
-
|
|
58
|
-
__Please help me build OSS__ 👉 [GitHub Sponsors](https://github.com/sponsors/typicode) :heart:
|
|
59
|
-
|
|
60
|
-
## Table of contents
|
|
61
|
-
|
|
62
|
-
<!-- toc -->
|
|
63
|
-
|
|
64
|
-
- [Getting started](#getting-started)
|
|
65
|
-
- [Routes](#routes)
|
|
66
|
-
* [Plural routes](#plural-routes)
|
|
67
|
-
* [Singular routes](#singular-routes)
|
|
68
|
-
* [Filter](#filter)
|
|
69
|
-
* [Paginate](#paginate)
|
|
70
|
-
* [Sort](#sort)
|
|
71
|
-
* [Slice](#slice)
|
|
72
|
-
* [Operators](#operators)
|
|
73
|
-
* [Full-text search](#full-text-search)
|
|
74
|
-
* [Relationships](#relationships)
|
|
75
|
-
* [Database](#database)
|
|
76
|
-
* [Homepage](#homepage)
|
|
77
|
-
- [Extras](#extras)
|
|
78
|
-
* [Static file server](#static-file-server)
|
|
79
|
-
* [Alternative port](#alternative-port)
|
|
80
|
-
* [Access from anywhere](#access-from-anywhere)
|
|
81
|
-
* [Remote schema](#remote-schema)
|
|
82
|
-
* [Generate random data](#generate-random-data)
|
|
83
|
-
* [HTTPS](#https)
|
|
84
|
-
* [Add custom routes](#add-custom-routes)
|
|
85
|
-
* [Add middlewares](#add-middlewares)
|
|
86
|
-
* [CLI usage](#cli-usage)
|
|
87
|
-
* [Module](#module)
|
|
88
|
-
+ [Simple example](#simple-example)
|
|
89
|
-
+ [Custom routes example](#custom-routes-example)
|
|
90
|
-
+ [Access control example](#access-control-example)
|
|
91
|
-
+ [Custom output example](#custom-output-example)
|
|
92
|
-
+ [Rewriter example](#rewriter-example)
|
|
93
|
-
+ [Mounting JSON Server on another endpoint example](#mounting-json-server-on-another-endpoint-example)
|
|
94
|
-
+ [API](#api)
|
|
95
|
-
* [Deployment](#deployment)
|
|
96
|
-
- [Links](#links)
|
|
97
|
-
* [Video](#video)
|
|
98
|
-
* [Articles](#articles)
|
|
99
|
-
* [Third-party tools](#third-party-tools)
|
|
100
|
-
- [License](#license)
|
|
101
|
-
|
|
102
|
-
<!-- tocstop -->
|
|
103
|
-
|
|
104
|
-
## Getting started
|
|
105
|
-
|
|
106
|
-
Install JSON Server
|
|
107
|
-
|
|
108
|
-
```
|
|
109
|
-
npm install -g json-server
|
|
10
|
+
```shell
|
|
11
|
+
npm install json-server@alpha
|
|
110
12
|
```
|
|
111
13
|
|
|
112
|
-
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
Create a `db.json` (or `db.json5`) file
|
|
113
17
|
|
|
114
18
|
```json
|
|
115
19
|
{
|
|
116
20
|
"posts": [
|
|
117
|
-
{ "id": 1, "title": "
|
|
21
|
+
{ "id": "1", "title": "a title" },
|
|
22
|
+
{ "id": "2", "title": "another title" }
|
|
118
23
|
],
|
|
119
24
|
"comments": [
|
|
120
|
-
{ "id": 1, "
|
|
25
|
+
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
|
|
26
|
+
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
|
|
121
27
|
],
|
|
122
|
-
"profile": {
|
|
28
|
+
"profile": {
|
|
29
|
+
"name": "typicode"
|
|
30
|
+
}
|
|
123
31
|
}
|
|
124
32
|
```
|
|
125
33
|
|
|
126
|
-
|
|
34
|
+
Pass it to JSON Server CLI
|
|
127
35
|
|
|
128
|
-
```
|
|
129
|
-
json-server
|
|
36
|
+
```shell
|
|
37
|
+
$ json-server db.json
|
|
130
38
|
```
|
|
131
39
|
|
|
132
|
-
|
|
40
|
+
Get a REST API
|
|
133
41
|
|
|
134
|
-
```
|
|
135
|
-
|
|
42
|
+
```shell
|
|
43
|
+
$ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
|
|
44
|
+
{
|
|
45
|
+
"id": "1",
|
|
46
|
+
"title": "a title"
|
|
47
|
+
}
|
|
136
48
|
```
|
|
137
49
|
|
|
138
|
-
|
|
50
|
+
Run `json-server --help` for a list of options
|
|
139
51
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
52
|
+
| Sponsors |
|
|
53
|
+
| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|
|
54
|
+
| <a href="https://mockend.com/" target="_blank"><img src="https://jsonplaceholder.typicode.com/mockend.svg" height="70px"></a> |
|
|
55
|
+
| <a href="https://www.storyblok.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/c6b10674-4ada-4616-91b8-59d30046b45a" height="40px"></a> |
|
|
144
56
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
Based on the previous `db.json` file, here are all the default routes. You can also add [other routes](#add-custom-routes) using `--routes`.
|
|
57
|
+
[Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship)
|
|
148
58
|
|
|
149
|
-
|
|
59
|
+
## Routes
|
|
150
60
|
|
|
151
61
|
```
|
|
152
62
|
GET /posts
|
|
153
|
-
GET /posts
|
|
63
|
+
GET /posts/:id
|
|
154
64
|
POST /posts
|
|
155
|
-
PUT /posts
|
|
156
|
-
PATCH /posts
|
|
157
|
-
DELETE /posts
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### Singular routes
|
|
161
|
-
|
|
162
|
-
```
|
|
163
|
-
GET /profile
|
|
164
|
-
POST /profile
|
|
165
|
-
PUT /profile
|
|
166
|
-
PATCH /profile
|
|
65
|
+
PUT /posts/:id
|
|
66
|
+
PATCH /posts/:id
|
|
67
|
+
DELETE /posts/:id
|
|
167
68
|
```
|
|
168
69
|
|
|
169
|
-
### Filter
|
|
170
|
-
|
|
171
|
-
Use `.` to access deep properties
|
|
172
|
-
|
|
173
70
|
```
|
|
174
|
-
GET
|
|
175
|
-
|
|
176
|
-
|
|
71
|
+
GET /profile
|
|
72
|
+
PUT /profile
|
|
73
|
+
PATCH /profile
|
|
177
74
|
```
|
|
178
75
|
|
|
179
|
-
|
|
76
|
+
## Params
|
|
180
77
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
In the `Link` header you'll get `first`, `prev`, `next` and `last` links.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
GET /posts?_page=7
|
|
188
|
-
GET /posts?_page=7&_limit=20
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
_10 items are returned by default_
|
|
192
|
-
|
|
193
|
-
### Sort
|
|
194
|
-
|
|
195
|
-
Add `_sort` and `_order` (ascending order by default)
|
|
196
|
-
|
|
197
|
-
```
|
|
198
|
-
GET /posts?_sort=views&_order=asc
|
|
199
|
-
GET /posts/1/comments?_sort=votes&_order=asc
|
|
200
|
-
```
|
|
78
|
+
### Conditions
|
|
201
79
|
|
|
202
|
-
|
|
80
|
+
- ` ` → `==`
|
|
81
|
+
- `lt` → `<`
|
|
82
|
+
- `lte` → `<=`
|
|
83
|
+
- `gt` → `>`
|
|
84
|
+
- `gte` → `>=`
|
|
85
|
+
- `ne` → `!=`
|
|
203
86
|
|
|
204
87
|
```
|
|
205
|
-
GET /posts?
|
|
88
|
+
GET /posts?views_gt=9000
|
|
206
89
|
```
|
|
207
90
|
|
|
208
|
-
###
|
|
91
|
+
### Range
|
|
209
92
|
|
|
210
|
-
|
|
93
|
+
- `start`
|
|
94
|
+
- `end`
|
|
95
|
+
- `limit`
|
|
211
96
|
|
|
212
97
|
```
|
|
213
|
-
GET /posts?_start=
|
|
214
|
-
GET /posts
|
|
215
|
-
GET /posts/1/comments?_start=20&_limit=10
|
|
98
|
+
GET /posts?_start=10&_end=20
|
|
99
|
+
GET /posts?_start=10&_limit=10
|
|
216
100
|
```
|
|
217
101
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
### Operators
|
|
102
|
+
### Paginate
|
|
221
103
|
|
|
222
|
-
|
|
104
|
+
- `page`
|
|
105
|
+
- `per_page` (default = 10)
|
|
223
106
|
|
|
224
107
|
```
|
|
225
|
-
GET /posts?
|
|
108
|
+
GET /posts?_page=1&_per_page=25
|
|
226
109
|
```
|
|
227
110
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
```
|
|
231
|
-
GET /posts?id_ne=1
|
|
232
|
-
```
|
|
111
|
+
### Sort
|
|
233
112
|
|
|
234
|
-
|
|
113
|
+
- `_sort=f1,f2`
|
|
235
114
|
|
|
236
115
|
```
|
|
237
|
-
GET /posts?
|
|
116
|
+
GET /posts?_sort=id,-views
|
|
238
117
|
```
|
|
239
118
|
|
|
240
|
-
###
|
|
119
|
+
### Nested and array fields
|
|
241
120
|
|
|
242
|
-
|
|
121
|
+
- `x.y.z...`
|
|
122
|
+
- `x.y.z[i]...`
|
|
243
123
|
|
|
244
124
|
```
|
|
245
|
-
GET /posts?
|
|
125
|
+
GET /posts?author.name=foo
|
|
126
|
+
GET /posts?author.email=foo
|
|
127
|
+
GET /posts?names[0]=foo
|
|
246
128
|
```
|
|
247
129
|
|
|
248
|
-
###
|
|
249
|
-
|
|
250
|
-
To include children resources, add `_embed`
|
|
130
|
+
### Embed
|
|
251
131
|
|
|
252
132
|
```
|
|
253
133
|
GET /posts?_embed=comments
|
|
254
|
-
GET /
|
|
255
|
-
```
|
|
256
|
-
|
|
257
|
-
To include parent resource, add `_expand`
|
|
258
|
-
|
|
259
|
-
```
|
|
260
|
-
GET /comments?_expand=post
|
|
261
|
-
GET /comments/1?_expand=post
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
To get or create nested resources (by default one level, [add custom routes](#add-custom-routes) for more)
|
|
265
|
-
|
|
266
|
-
```
|
|
267
|
-
GET /posts/1/comments
|
|
268
|
-
POST /posts/1/comments
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
### Database
|
|
272
|
-
|
|
273
|
-
```
|
|
274
|
-
GET /db
|
|
275
|
-
```
|
|
276
|
-
|
|
277
|
-
### Homepage
|
|
278
|
-
|
|
279
|
-
Returns default index file or serves `./public` directory
|
|
280
|
-
|
|
281
|
-
```
|
|
282
|
-
GET /
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
## Extras
|
|
286
|
-
|
|
287
|
-
### Static file server
|
|
288
|
-
|
|
289
|
-
You can use JSON Server to serve your HTML, JS and CSS, simply create a `./public` directory
|
|
290
|
-
or use `--static` to set a different static files directory.
|
|
291
|
-
|
|
292
|
-
```bash
|
|
293
|
-
mkdir public
|
|
294
|
-
echo 'hello world' > public/index.html
|
|
295
|
-
json-server db.json
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
```bash
|
|
299
|
-
json-server db.json --static ./some-other-dir
|
|
134
|
+
GET /comments?_embed=post
|
|
300
135
|
```
|
|
301
136
|
|
|
302
|
-
|
|
137
|
+
## Delete
|
|
303
138
|
|
|
304
|
-
You can start JSON Server on other ports with the `--port` flag:
|
|
305
|
-
|
|
306
|
-
```bash
|
|
307
|
-
$ json-server --watch db.json --port 3004
|
|
308
139
|
```
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
You can access your fake API from anywhere using CORS and JSONP.
|
|
313
|
-
|
|
314
|
-
### Remote schema
|
|
315
|
-
|
|
316
|
-
You can load remote schemas.
|
|
317
|
-
|
|
318
|
-
```bash
|
|
319
|
-
$ json-server http://example.com/file.json
|
|
320
|
-
$ json-server http://jsonplaceholder.typicode.com/db
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
### Generate random data
|
|
324
|
-
|
|
325
|
-
Using JS instead of a JSON file, you can create data programmatically.
|
|
326
|
-
|
|
327
|
-
```javascript
|
|
328
|
-
// index.js
|
|
329
|
-
module.exports = () => {
|
|
330
|
-
const data = { users: [] }
|
|
331
|
-
// Create 1000 users
|
|
332
|
-
for (let i = 0; i < 1000; i++) {
|
|
333
|
-
data.users.push({ id: i, name: `user${i}` })
|
|
334
|
-
}
|
|
335
|
-
return data
|
|
336
|
-
}
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
```bash
|
|
340
|
-
$ json-server index.js
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
__Tip__ use modules like [Faker](https://github.com/faker-js/faker), [Casual](https://github.com/boo1ean/casual), [Chance](https://github.com/victorquinn/chancejs) or [JSON Schema Faker](https://github.com/json-schema-faker/json-schema-faker).
|
|
344
|
-
|
|
345
|
-
### HTTPS
|
|
346
|
-
|
|
347
|
-
There are many ways to set up SSL in development. One simple way is to use [hotel](https://github.com/typicode/hotel).
|
|
348
|
-
|
|
349
|
-
### Add custom routes
|
|
350
|
-
|
|
351
|
-
Create a `routes.json` file. Pay attention to start every route with `/`.
|
|
352
|
-
|
|
353
|
-
```json
|
|
354
|
-
{
|
|
355
|
-
"/api/*": "/$1",
|
|
356
|
-
"/:resource/:id/show": "/:resource/:id",
|
|
357
|
-
"/posts/:category": "/posts?category=:category",
|
|
358
|
-
"/articles?id=:id": "/posts/:id"
|
|
359
|
-
}
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
Start JSON Server with `--routes` option.
|
|
363
|
-
|
|
364
|
-
```bash
|
|
365
|
-
json-server db.json --routes routes.json
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
Now you can access resources using additional routes.
|
|
369
|
-
|
|
370
|
-
```sh
|
|
371
|
-
/api/posts # → /posts
|
|
372
|
-
/api/posts/1 # → /posts/1
|
|
373
|
-
/posts/1/show # → /posts/1
|
|
374
|
-
/posts/javascript # → /posts?category=javascript
|
|
375
|
-
/articles?id=1 # → /posts/1
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
### Add middlewares
|
|
379
|
-
|
|
380
|
-
You can add your middlewares from the CLI using `--middlewares` option:
|
|
381
|
-
|
|
382
|
-
```js
|
|
383
|
-
// hello.js
|
|
384
|
-
module.exports = (req, res, next) => {
|
|
385
|
-
res.header('X-Hello', 'World')
|
|
386
|
-
next()
|
|
387
|
-
}
|
|
388
|
-
```
|
|
389
|
-
|
|
390
|
-
```bash
|
|
391
|
-
json-server db.json --middlewares ./hello.js
|
|
392
|
-
json-server db.json --middlewares ./first.js ./second.js
|
|
393
|
-
```
|
|
394
|
-
|
|
395
|
-
### CLI usage
|
|
396
|
-
|
|
397
|
-
```
|
|
398
|
-
json-server [options] <source>
|
|
399
|
-
|
|
400
|
-
Options:
|
|
401
|
-
--config, -c Path to config file [default: "json-server.json"]
|
|
402
|
-
--port, -p Set port [default: 3000]
|
|
403
|
-
--host, -H Set host [default: "localhost"]
|
|
404
|
-
--watch, -w Watch file(s) [boolean]
|
|
405
|
-
--routes, -r Path to routes file
|
|
406
|
-
--middlewares, -m Paths to middleware files [array]
|
|
407
|
-
--static, -s Set static files directory
|
|
408
|
-
--read-only, --ro Allow only GET requests [boolean]
|
|
409
|
-
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
|
|
410
|
-
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
|
|
411
|
-
--snapshots, -S Set snapshots directory [default: "."]
|
|
412
|
-
--delay, -d Add delay to responses (ms)
|
|
413
|
-
--id, -i Set database id property (e.g. _id) [default: "id"]
|
|
414
|
-
--foreignKeySuffix, --fks Set foreign key suffix, (e.g. _id as in post_id)
|
|
415
|
-
[default: "Id"]
|
|
416
|
-
--quiet, -q Suppress log messages from output [boolean]
|
|
417
|
-
--help, -h Show help [boolean]
|
|
418
|
-
--version, -v Show version number [boolean]
|
|
419
|
-
|
|
420
|
-
Examples:
|
|
421
|
-
json-server db.json
|
|
422
|
-
json-server file.js
|
|
423
|
-
json-server http://example.com/db.json
|
|
424
|
-
|
|
425
|
-
https://github.com/typicode/json-server
|
|
426
|
-
```
|
|
427
|
-
|
|
428
|
-
You can also set options in a `json-server.json` configuration file.
|
|
429
|
-
|
|
430
|
-
```json
|
|
431
|
-
{
|
|
432
|
-
"port": 3000
|
|
433
|
-
}
|
|
140
|
+
DELETE /posts/1
|
|
141
|
+
DELETE /posts/1?_embed=comments
|
|
434
142
|
```
|
|
435
143
|
|
|
436
|
-
|
|
144
|
+
## Serving static files
|
|
437
145
|
|
|
438
|
-
If you
|
|
146
|
+
If you create a `./public` directory, JSON Serve will serve its content in addition to the REST API.
|
|
439
147
|
|
|
440
|
-
|
|
148
|
+
You can also add custom directories using `-s/--static` option.
|
|
441
149
|
|
|
442
150
|
```sh
|
|
443
|
-
|
|
151
|
+
json-server -s ./static
|
|
152
|
+
json-server -s ./static -s ./node_modules
|
|
444
153
|
```
|
|
445
154
|
|
|
446
|
-
```js
|
|
447
|
-
// server.js
|
|
448
|
-
const jsonServer = require('json-server')
|
|
449
|
-
const server = jsonServer.create()
|
|
450
|
-
const router = jsonServer.router('db.json')
|
|
451
|
-
const middlewares = jsonServer.defaults()
|
|
452
|
-
|
|
453
|
-
server.use(middlewares)
|
|
454
|
-
server.use(router)
|
|
455
|
-
server.listen(3000, () => {
|
|
456
|
-
console.log('JSON Server is running')
|
|
457
|
-
})
|
|
458
|
-
```
|
|
459
|
-
|
|
460
|
-
```sh
|
|
461
|
-
$ node server.js
|
|
462
|
-
```
|
|
463
|
-
|
|
464
|
-
The path you provide to the `jsonServer.router` function is relative to the directory from where you launch your node process. If you run the above code from another directory, it’s better to use an absolute path:
|
|
465
|
-
|
|
466
|
-
```js
|
|
467
|
-
const path = require('path')
|
|
468
|
-
const router = jsonServer.router(path.join(__dirname, 'db.json'))
|
|
469
|
-
```
|
|
470
|
-
|
|
471
|
-
For an in-memory database, simply pass an object to `jsonServer.router()`.
|
|
472
|
-
|
|
473
|
-
To add custom options (eg. `foreginKeySuffix`) pass in an object as the second argument to `jsonServer.router('db.json', { foreginKeySuffix: '_id' })`.
|
|
474
|
-
|
|
475
|
-
Please note also that `jsonServer.router()` can be used in existing Express projects.
|
|
476
|
-
|
|
477
|
-
#### Custom routes example
|
|
478
|
-
|
|
479
|
-
Let's say you want a route that echoes query parameters and another one that set a timestamp on every resource created.
|
|
480
|
-
|
|
481
|
-
```js
|
|
482
|
-
const jsonServer = require('json-server')
|
|
483
|
-
const server = jsonServer.create()
|
|
484
|
-
const router = jsonServer.router('db.json')
|
|
485
|
-
const middlewares = jsonServer.defaults()
|
|
486
|
-
|
|
487
|
-
// Set default middlewares (logger, static, cors and no-cache)
|
|
488
|
-
server.use(middlewares)
|
|
489
|
-
|
|
490
|
-
// Add custom routes before JSON Server router
|
|
491
|
-
server.get('/echo', (req, res) => {
|
|
492
|
-
res.jsonp(req.query)
|
|
493
|
-
})
|
|
494
|
-
|
|
495
|
-
// To handle POST, PUT and PATCH you need to use a body-parser
|
|
496
|
-
// You can use the one used by JSON Server
|
|
497
|
-
server.use(jsonServer.bodyParser)
|
|
498
|
-
server.use((req, res, next) => {
|
|
499
|
-
if (req.method === 'POST') {
|
|
500
|
-
req.body.createdAt = Date.now()
|
|
501
|
-
}
|
|
502
|
-
// Continue to JSON Server router
|
|
503
|
-
next()
|
|
504
|
-
})
|
|
505
|
-
|
|
506
|
-
// Use default router
|
|
507
|
-
server.use(router)
|
|
508
|
-
server.listen(3000, () => {
|
|
509
|
-
console.log('JSON Server is running')
|
|
510
|
-
})
|
|
511
|
-
```
|
|
512
|
-
|
|
513
|
-
#### Access control example
|
|
514
|
-
|
|
515
|
-
```js
|
|
516
|
-
const jsonServer = require('json-server')
|
|
517
|
-
const server = jsonServer.create()
|
|
518
|
-
const router = jsonServer.router('db.json')
|
|
519
|
-
const middlewares = jsonServer.defaults()
|
|
520
|
-
|
|
521
|
-
server.use(middlewares)
|
|
522
|
-
server.use((req, res, next) => {
|
|
523
|
-
if (isAuthorized(req)) { // add your authorization logic here
|
|
524
|
-
next() // continue to JSON Server router
|
|
525
|
-
} else {
|
|
526
|
-
res.sendStatus(401)
|
|
527
|
-
}
|
|
528
|
-
})
|
|
529
|
-
server.use(router)
|
|
530
|
-
server.listen(3000, () => {
|
|
531
|
-
console.log('JSON Server is running')
|
|
532
|
-
})
|
|
533
|
-
```
|
|
534
|
-
#### Custom output example
|
|
535
|
-
|
|
536
|
-
To modify responses, overwrite `router.render` method:
|
|
537
|
-
|
|
538
|
-
```javascript
|
|
539
|
-
// In this example, returned resources will be wrapped in a body property
|
|
540
|
-
router.render = (req, res) => {
|
|
541
|
-
res.jsonp({
|
|
542
|
-
body: res.locals.data
|
|
543
|
-
})
|
|
544
|
-
}
|
|
545
|
-
```
|
|
546
|
-
|
|
547
|
-
You can set your own status code for the response:
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
```javascript
|
|
551
|
-
// In this example we simulate a server side error response
|
|
552
|
-
router.render = (req, res) => {
|
|
553
|
-
res.status(500).jsonp({
|
|
554
|
-
error: "error message here"
|
|
555
|
-
})
|
|
556
|
-
}
|
|
557
|
-
```
|
|
558
|
-
|
|
559
|
-
#### Rewriter example
|
|
560
|
-
|
|
561
|
-
To add rewrite rules, use `jsonServer.rewriter()`:
|
|
562
|
-
|
|
563
|
-
```javascript
|
|
564
|
-
// Add this before server.use(router)
|
|
565
|
-
server.use(jsonServer.rewriter({
|
|
566
|
-
'/api/*': '/$1',
|
|
567
|
-
'/blog/:resource/:id/show': '/:resource/:id'
|
|
568
|
-
}))
|
|
569
|
-
```
|
|
570
|
-
|
|
571
|
-
#### Mounting JSON Server on another endpoint example
|
|
572
|
-
|
|
573
|
-
Alternatively, you can also mount the router on `/api`.
|
|
574
|
-
|
|
575
|
-
```javascript
|
|
576
|
-
server.use('/api', router)
|
|
577
|
-
```
|
|
578
|
-
|
|
579
|
-
#### API
|
|
580
|
-
|
|
581
|
-
__`jsonServer.create()`__
|
|
582
|
-
|
|
583
|
-
Returns an Express server.
|
|
584
|
-
|
|
585
|
-
__`jsonServer.defaults([options])`__
|
|
586
|
-
|
|
587
|
-
Returns middlewares used by JSON Server.
|
|
588
|
-
|
|
589
|
-
* options
|
|
590
|
-
* `static` path to static files
|
|
591
|
-
* `logger` enable logger middleware (default: true)
|
|
592
|
-
* `bodyParser` enable body-parser middleware (default: true)
|
|
593
|
-
* `noCors` disable CORS (default: false)
|
|
594
|
-
* `readOnly` accept only GET requests (default: false)
|
|
595
|
-
|
|
596
|
-
__`jsonServer.router([path|object], [options])`__
|
|
597
|
-
|
|
598
|
-
Returns JSON Server router.
|
|
599
|
-
|
|
600
|
-
* options (see [CLI usage](#cli-usage))
|
|
601
|
-
|
|
602
|
-
### Deployment
|
|
603
|
-
|
|
604
|
-
You can deploy JSON Server. For example, [JSONPlaceholder](http://jsonplaceholder.typicode.com) is an online fake API powered by JSON Server and running on Heroku.
|
|
605
|
-
|
|
606
|
-
## Links
|
|
607
|
-
|
|
608
|
-
### Video
|
|
609
|
-
|
|
610
|
-
* [Creating Demo APIs with json-server on egghead.io](https://egghead.io/lessons/nodejs-creating-demo-apis-with-json-server)
|
|
611
|
-
|
|
612
|
-
### Articles
|
|
613
|
-
|
|
614
|
-
* [Node Module Of The Week - json-server](http://nmotw.in/json-server/)
|
|
615
|
-
* [ng-admin: Add an AngularJS admin GUI to any RESTful API](http://marmelab.com/blog/2014/09/15/easy-backend-for-your-restful-api.html)
|
|
616
|
-
* [Fast prototyping using Restangular and Json-server](https://glebbahmutov.com/blog/fast-prototyping-restangular-and-json-server/)
|
|
617
|
-
* [Create a Mock REST API in Seconds for Prototyping your Frontend](https://coligo.io/create-mock-rest-api-with-json-server/)
|
|
618
|
-
* [No API? No Problem! Rapid Development via Mock APIs](https://medium.com/@housecor/rapid-development-via-mock-apis-e559087be066#.93d7w8oro)
|
|
619
|
-
* [Zero Code REST With json-server](https://dzone.com/articles/zero-code-rest-with-json-server)
|
|
620
|
-
|
|
621
|
-
### Third-party tools
|
|
622
|
-
|
|
623
|
-
* [Grunt JSON Server](https://github.com/tfiwm/grunt-json-server)
|
|
624
|
-
* [Docker JSON Server](https://github.com/clue/docker-json-server)
|
|
625
|
-
* [JSON Server GUI](https://github.com/naholyr/json-server-gui)
|
|
626
|
-
* [JSON file generator](https://github.com/dfsq/json-server-init)
|
|
627
|
-
* [JSON Server extension](https://github.com/maty21/json-server-extension)
|
|
628
|
-
|
|
629
155
|
## License
|
|
630
156
|
|
|
631
|
-
|
|
157
|
+
This project uses the [Fair Source License](https://fair.io/). Note: Only organizations with 3+ users need 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.
|
|
632
158
|
|
|
633
|
-
[
|
|
159
|
+
For more information, FAQs, and the rationale behind this, visit [https://fair.io/](https://fair.io/).
|