简介
JSON Server 是一个非常实用的工具,可以让你快速搭建一个模拟 REST API。它可以基于一个 JSON 文件快速创建一个全功能的假 REST API,非常适合前端开发时做原型或 mock 数据。本文将详细介绍 JSON Server 的安装和使用方法。
安装
JSON Server 是一个 Node.js 模块,可以通过 npm 安装:
npm install -g json-server
创建 JSON 数据文件
首先创建一个 JSON 文件,例如 db.json:
{
"posts": [
{ "id": 1, "title": "json-server", "author": "typicode" }
],
"comments": [
{ "id": 1, "body": "some comment", "postId": 1 }
],
"profile": { "name": "typicode" }
}
启动服务器
在命令行中,进入 db.json 所在目录,运行:
json-server --watch db.json
现在,你就可以通过 http://localhost:3000 访问你的 API 了。
API 路由
JSON Server 会根据你的 JSON 文件自动生成以下路由:
- GET /posts
- GET /posts/1
- POST /posts
- PUT /posts/1
- PATCH /posts/1
- DELETE /posts/1
类似地,其他顶级属性(如 comments, profi
JSON Server 详细使用教程

最低0.47元/天 解锁文章
769

被折叠的 条评论
为什么被折叠?



