json-server v1.0.0-beta.3
json-server
!IMPORTANT Viewing beta v1 documentation ā usable but expect breaking changes. For stable version, see here
š Hey! Using React, Vue or Astro? Check my new project MistCSS to write 50% less code.
Install
npm install json-serverUsage
Create a db.json or db.json5 file
{
"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"
}
}{
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.
Pass it to JSON Server CLI
$ npx json-server db.jsonGet a REST API
$ curl http://localhost:3000/posts/1
{
"id": "1",
"title": "a title",
"views": 100
}Run json-server --help for a list of options
Sponsors āØ
| Sponsors |
|---|
| Sponsors |
|---|
| Sponsors | |
|---|---|
Become a sponsor and have your company logo here
Sponsorware
!NOTE This project uses the Fair Source License. Only organizations with 3+ users are kindly asked to contribute a small amount through sponsorship sponsor 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/.
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 commentsGET /profile
PUT /profile
PATCH /profileParams
Conditions
ā==ltā<lteā<=gtā>gteā>=neā!=
GET /posts?views_gt=9000Range
startendlimit
GET /posts?_start=10&_end=20
GET /posts?_start=10&_limit=10Paginate
pageper_page(default = 10)
GET /posts?_page=1&_per_page=25Sort
_sort=f1,f2
GET /posts?_sort=id,-viewsNested 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]=barEmbed
GET /posts?_embed=comments
GET /comments?_embed=postDelete
DELETE /posts/1
DELETE /posts/1?_dependent=commentsServing static files
If you create a ./public directory, JSON Server will serve its content in addition to the REST API.
You can also add custom directories using -s/--static option.
json-server -s ./static
json-server -s ./static -s ./node_modulesNotable differences with v0.17
idis always a string and will be generated for you if missing- use
_per_pagewith_pageinstead of_limitfor pagination - use Chrome's
Network tab > throtlingto delay requests instead of--delayCLI option
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
4 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago