2023-12-30 01:48:38 +01:00
2023-11-27 23:41:42 +01:00
2023-12-26 01:20:30 +01:00
2023-12-30 01:47:49 +01:00
2023-12-30 01:47:49 +01:00
wip
2023-11-27 23:41:46 +01:00
2021-08-02 03:20:19 +02:00
2023-12-01 15:14:14 +01:00
2023-12-17 14:55:03 +01:00
wip
2023-11-27 23:41:46 +01:00
2023-12-30 01:48:38 +01:00
2023-12-30 01:48:38 +01:00
2023-12-30 01:32:39 +01:00
wip
2023-11-27 23:41:46 +01:00

json-server

Node.js CI

Install

npm install json-server@alpha

Usage

Create a db.json (or db.json5) file

{
  "posts": [
    { "id": "1", "title": "a title" },
    { "id": "2", "title": "another title" }
  ],
  "comments": [
    { "id": "1", "text": "a comment about post 1", "postId": "1" },
    { "id": "2", "text": "another comment about post 1", "postId": "1" }
  ]
}

Pass it to JSON Server CLI

$ json-server db.json

Get a REST API

$ curl -H "Accept: application/json" -X GET http://localhost:3000/posts/1
{
  "id": "1",
  "title": "a title"
}

Run json-server --help for a list of options

Routes

GET    /posts
GET    /posts/:id
POST   /posts
PUT    /posts/:id
PATCH  /posts/:id
DELETE /posts/:id

Params

Comparison

  • ==
  • 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 /posts?author.name=foo
GET /posts?author.email=foo
GET /posts?names[0]=foo

Include

GET /posts?_include=comments
GET /comments?_include=post

Delete

DELETE /posts/1
DELETE /posts/1?_include=comments

Serving static files

If you create a ./public directory, JSON Serve will serve its content in addition to the REST API. You can add custom directories using -s/--static option.

json-server -s ./static
json-server -s ./static -s ./node_modules

License

This project uses the Fair Source License. Note: Only organizations with 3+ users need 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/.

Description
Get a full fake REST API with zero coding in less than 30 seconds (seriously)
Readme 10 MiB
Languages
JavaScript 94.7%
HTML 5.3%