JSON服务器 (json-server)
今天我们将介绍一个非常实用的工具json-server,它可以在一分钟内为你提供一个模拟的rest json服务器。在一个常规的企业应用中,你会与许多团队和第三方API一起工作。想象一下,你必须调用一个第三方RESTful Web服务来获取JSON数据进行处理。你的时间表很紧,所以你不能等待他们完成工作再开始你自己的工作。如果你希望有一个模拟的Rest Web服务来获取演示数据,那么json-server是你在寻找的工具。
JSON服务器
安装JSON服务器
你的机器上应该安装有NPM。如果没有安装,请参考这篇文章来安装NPM。下面展示了在我的机器上安装json-server的一行命令及其输出。
$ npm install -g json-server
npm WARN deprecated graceful-fs@3.0.8: graceful-fs v3.0.0 and before will fail on node releases >= v7.0. Please update to graceful-fs@^4.0.0 as soon as possible. Use 'npm ls graceful-fs' to find it in the tree.
/usr/local/bin/json-server -> /usr/local/lib/node_modules/json-server/bin/index.js
- bytes@2.3.0 node_modules/json-server/node_modules/raw-body/node_modules/bytes
/usr/local/lib
└─┬ json-server@0.8.10
├─┬ body-parser@1.15.1
│ └── bytes@2.3.0
├─┬ compression@1.6.1
│ └── bytes@2.2.0
├─┬ lowdb@0.10.3
│ └─┬ steno@0.4.4
│ └── graceful-fs@4.1.4
├─┬ update-notifier@0.5.0
│ └─┬ configstore@1.4.0
│ ├── graceful-fs@4.1.4
│ └─┬ write-file-atomic@1.1.4
│ └── graceful-fs@4.1.4
└─┬ yargs@4.7.0
├─┬ pkg-conf@1.1.2
│ └─┬ load-json-file@1.1.0
│ └── graceful-fs@4.1.4
└─┬ read-pkg-up@1.0.1
└─┬ read-pkg@1.1.0
└─┬ path-type@1.1.0
└── graceful-fs@4.1.4
$
检查 json-server 的版本和选项
$ json-server -v
0.8.10
$ json-server -help
/usr/local/bin/json-server [options] <source>
Options:
--config, -c Path to config file [default: "json-server.json"]
--port, -p Set port [default: 3000]
--host, -H Set host [default: "0.0.0.0"]
--watch, -w Watch file(s) [boolean]
--routes, -r Path to routes file
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [boolean]
--no-cors, --nc Disable Cross-Origin Resource Sharing [boolean]
--no-gzip, --ng Disable GZIP Content-Encoding [boolean]
--snapshots, -S Set snapshots directory [default: "."]
--delay, -d Add delay to responses (ms)
--id, -i Set database id property (e.g. _id) [default: "id"]
--quiet, -q Suppress log messages from output [boolean]
$
运行JSON服务器
现在是时候启动我们的json服务器了。下面是一个包含我的员工json数据的示例文件。
{
"employees": [
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "David",
"salary": "5000",
"id": 2
}
]
}
这里的重要一点是数组的名称,即员工 (employees)。JSON服务器将根据这个名称创建REST API。让我们使用以上文件启动我们的json-server。
$ json-server --watch db.json
\{^_^}/ hi!
Loading db.json
Done
Resources
https://localhost:3000/employees
Home
https://localhost:3000
Type s + enter at any time to create a snapshot of the database
Watching...
请不要关闭这个终端,否则会终止 json-server 的运行。以下是示例的 CRUD 请求和响应。
JSON服务器GET – 读取所有员工
$ curl -X GET -H "Content-Type: application/json" "https://localhost:3000/employees"
[
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "David",
"salary": "5000",
"id": 2
}
]
$
从json-server根据ID获取员工
$ curl -X GET -H "Content-Type: application/json" "https://localhost:3000/employees/1"
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
}
$
JSON服务器POST – 创建一个员工
$ curl -X POST -H "Content-Type: application/json" -d '{"name": "Lisa","salary": "2000"}' "https://localhost:3000/employees"
{
"name": "Lisa",
"salary": 2000,
"id": 3
}
$
JSON服务器PUT方法 – 更新员工数据
$ curl -XPUT -H "Content-Type: application/json" -d '{"name": "Lisa", "salary": "8000"}' "https://localhost:3000/employees/3"
{
"name": "Lisa",
"salary": 8000,
"id": 3
}
$
JSON服务器DELETE – 删除一个员工
$ curl -X DELETE -H "Content-Type: application/json" "https://localhost:3000/employees/2"
{}
$ curl -GET -H "Content-Type: application/json" "https://localhost:3000/employees"
[
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "Lisa",
"salary": 8000,
"id": 3
}
]
$
正如你所见,通过简单的JSON,json-server为我们创建了演示API供我们使用。请注意,所有的PUT、POST、DELETE请求都被保存在db.json文件中。现在GET和DELETE的URI相同,同样,POST和PUT请求的URI也相同。嗯,我们也可以使用一个简单的映射文件来创建自定义的URI。
定制的路由的 JSON 服务器
创建一个文件,用于我们的json-server来使用自定义路由。routes.json
{
"/employees/list": "/employees",
"/employees/get/:id": "/employees/:id",
"/employees/create": "/employees",
"/employees/update/:id": "/employees/:id",
"/employees/delete/:id": "/employees/:id"
}
我们还可以更改json-server端口,模拟第三方API,只需在真正的服务准备好时更改基本URL就可以继续使用。现在按照以下示例再次启动JSON服务器。
$ json-server --port 7000 --routes routes.json --watch db.json
(node:60899) fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module, please update it to a more recent version.
\{^_^}/ hi!
Loading db.json
Loading routes.json
Done
Resources
https://localhost:7000/employees
Other routes
/employees/list -> /employees
/employees/get/:id -> /employees/:id
/employees/create -> /employees
/employees/update/:id -> /employees/:id
/employees/delete/:id -> /employees/:id
Home
https://localhost:7000
Type s + enter at any time to create a snapshot of the database
Watching...
这显示的是我们定义的自定义路线。
用自定义的路由示例的json-server
以下是一些自定义路由命令及其输出的示例。
$ curl -X GET -H "Content-Type: application/json" "https://localhost:7000/employees/list"
[
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "Lisa",
"salary": 8000,
"id": 3
}
]
$ curl -X GET -H "Content-Type: application/json" "https://localhost:7000/employees/get/1"
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
}
$ curl -X POST -H "Content-Type: application/json" -d '{"name": "Lisa","salary": "2000"}' "https://localhost:7000/employees/create"
{
"name": "Lisa",
"salary": 2000,
"id": 4
}
$ curl -XPUT -H "Content-Type: application/json" -d '{"name": "Lisa", "salary": "8000"}' "https://localhost:7000/emloyees/update/4"
{
"name": "Lisa",
"salary": 8000,
"id": 4
}
$ curl -XDELETE -H "Content-Type: application/json" "https://localhost:7000/employees/delete/4"
{}
$ curl -GET -H "Content-Type: application/json" "https://localhost:7000/employees/list"
[
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "Lisa",
"salary": 8000,
"id": 3
}
]
$
JSON服务器提供了一些其他有用的选项,例如排序、搜索和分页。这就是json-server的全部内容,每当我需要创建演示Rest JSON API时,这是我首选的工具。参考:json-server GitHub。