在不到 30 秒内获得完全的虚拟 REST API,无需编写任何代码(真的)
npm install json-server
## 使用 创建 `db.json` 或 `db.json5` 文件json
{
"$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"
}
}
<details> <summary>查看 db.json5 示例</summary>json5
{
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",
},
}
您可以在 [这里](https://github.com/json5/json5) 详细了解 JSON5 格式。 </details> 启动 JSON Serverbash
npx json-server db.json
这将在 `http://localhost:3000`.` 上启动服务器。 您应该看到:
JSON Server started on PORT :3000
http://localhost:3000
访问您的 REST API:bash
curl http://localhost:3000/posts/1
**响应:**json
{
"id": "1",
"title": "a title",
"views": 100
}
``` 请运行 json-server --help 以查看选项列表 ## 赞助商 ✨ ### 黄金
| <a href="http:// |
负责任的披露
从日志中隐藏成功的 XHR 请求
对
DELETE 删除的条目多于它应该删除的条目
全文搜索无法正常运行
在 db.json 中开始 Json-Server 错误
在 node_modules 中找不到 json-server 包
请添加关于 json 数据变化的 Server- Send- Events
使用 _embed 与 persons 表时产生意外输出
特性: 当 db 包含原始数组作为资源时, 添加可选启动警告