json-server README

The project's README from github.com/typicode/json-server. As one markdown file: README.md.

JSON-Server

Node.js CI

[!IMPORTANT] Viewing beta v1 documentation – usable but expect breaking changes. For stable version, see here

[!NOTE] Using React ⚛️ and tired of CSS-in-JS? See MistCSS 👀

Install

npm install json-server

Usage

Create a db.json or db.json5 file

{
  "$schema": "./node_modules/json-server/schema.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"
  }
}
View db.json5 example
{
  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.

Start JSON Server

npx json-server db.json

This starts the server at http://localhost:3000. You should see:

JSON Server started on PORT :3000
http://localhost:3000

Access your REST API:

curl http://localhost:3000/posts/1

Response:

{
  "id": "1",
  "title": "a title",
  "views": 100
}

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

Sponsors ✨

Gold

tower-dock-icon-light

Silver

Bronze

Become a sponsor and have your company logo here

Query Capabilities

JSON Server supports advanced querying out of the box:

GET /posts?views:gt=100                  # Filter by condition
GET /posts?_sort=-views                  # Sort by field (descending)
GET /posts?_page=1&_per_page=10          # Pagination
GET /posts?_embed=comments               # Include relations
GET /posts?_where={"or":[...]}           # Complex queries

See detailed documentation below for each feature.

Routes

Array Resources

For array resources like posts and comments:

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

Object Resources

For singular object resources like profile:

GET   /profile
PUT   /profile
PATCH /profile

Query params

Conditions

Use field:operator=value.

Operators:

Examples:

GET /posts?views:gt=100
GET /posts?title:eq=Hello
GET /posts?id:in=1,2,3
GET /posts?author.name:eq=typicode
GET /posts?title:contains=hello
GET /posts?title:startsWith=Hello
GET /posts?title:endsWith=world

Sort

GET /posts?_sort=title
GET /posts?_sort=-views
GET /posts?_sort=author.name,-views

Pagination

GET /posts?_page=1&_per_page=25

Response:

{
  "first": 1,
  "prev": null,
  "next": 2,
  "last": 4,
  "pages": 4,
  "items": 100,
  "data": [
    { "id": "1", "title": "...", "views": 100 },
    { "id": "2", "title": "...", "views": 200 }
  ]
}

Notes:

Embed

GET /posts?_embed=comments
GET /comments?_embed=post

Complex filter with _where

_where accepts a JSON object and overrides normal query params when valid.

GET /posts?_where={"or":[{"views":{"gt":100}},{"author":{"name":{"lt":"m"}}}]}

Delete dependents

DELETE /posts/1?_dependent=comments

Static Files

JSON Server automatically serves files from the ./public directory.

To serve additional static directories:

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

Static files are served with standard MIME types and can include HTML, CSS, JavaScript, images, and other assets.

Migration Notes (v0 → v1)

If you are upgrading from json-server v0.x, note these behavioral changes:

New to json-server? These notes are for users migrating from v0. If this is your first time using json-server, you can ignore this section.

json-server (typicode/json-server)

Searching Database…

这是什么?json-server 可以在 30 秒内搭建一个功能完整的假 REST API,无需编写任何后端代码,只需一个定义数据的 JSON 文件即可。
这是需要我导入的库,还是独立的命令行工具?它是一个独立的命令行工具。你把它指向一个 JSON 文件,它就会立即提供 REST 接口;无需导入到你自己的代码中。
我该怎么把它跑起来?创建一个 db.json 或 db.json5 文件,描述你的资源(例如 posts、comments),然后让 json-server 指向该文件,即可在本地启动服务器。
我需要先安装什么?你需要 Node.js 22.12 或更高版本,因为这是 json-server 运行所需的最低引擎版本。
当前文档稳定吗?你看到的是 beta v1 文档。日常使用没问题,但 README 提醒,在正式定稿前仍可能出现不兼容的变更;生产环境请使用单独的稳定版本。
我可以对这个假 API 执行哪些查询?你可以使用 field:operator=value 的格式进行过滤,支持的运算符包括 eq、ne、lt、lte、gt、gte、in、contains、startsWith 和 endsWith;还可以对结果排序、用 page/perpage 进行分页,并通过 JSON 格式的 where 对象执行复杂过滤。
除了 JSON API,它还能提供其他内容吗?可以。json-server 开箱即用,会自动提供 ./public 目录中的静态文件,你还可以注册其他静态目录,用于提供 HTML、CSS、JS 或图片。
如何建立资源之间的关系?posts 和 comments 这类数组资源可以通过 embed 查询参数嵌入到响应中,它取代了 v0 版本中较旧的 expand 选项。
如果从旧的 v0 版本升级,有哪些变化?ID 现在始终为字符串,缺失时会自动生成;分页改用 perpage+page,取代了旧的 limit 参数;expand 被 embed 取代;--delay 命令行参数已被移除,改用浏览器开发者工具的网络限速功能。
What are similar projects?nock, msw, node-restify, ky, yapi, jest, cli, insomnia
Creator
typicode
Size
4.2 MB
Stars
75.7K
Age
12y
Commits · 90 Days