json-server可以不写一行代码在30秒内创建一套RESTFUL风格的fake api。换句话说,json-server可以迅速实现一套mock server或者叫做挡板逻辑,适合3人及以下的前端团队做迅速mock后台逻辑。
对于测试同学来说,不写代码就可以获取一套mock api有时候也是非常方便的事情,由于学习成本不高,所以我们可以花点时间去学习一下。
安装
首先需要安装nodejs。
国内的同学建议安装cnpm。
完成上面的步骤之后,在命令行使用下面的命令安装json-server
$ cnpm install -g json-server
验证安装
安装完成之后,在命令行运行
json-server -h
如果正确安装,会展示下面的信息
json-server [options] <source>
选项:
--config, -c Path to config file [默认值: "json-server.json"]
--port, -p Set port [默认值: 3000]
--host, -H Set host [默认值: "0.0.0.0"]
--watch, -w Watch file(s) [布尔]
--routes, -r Path to routes file
--middlewares, -m Paths to middleware files [数组]
--static, -s Set static files directory
--read-only, --ro Allow only GET requests [布尔]
--no-cors, --nc Disable Cross-Origin Resource Sharing [布尔]
--no-gzip, --ng Disable GZIP Content-Encoding [布尔]
--snapshots, -S Set snapshots directory [默认值: "."]
--delay, -d Add delay to responses (ms)
--id, -i Set database id property (e.g. _id) [默认值: "id"]
--foreignKeySuffix, --fks Set foreign key suffix (e.g. _id as in post_id)
[默认值: "Id"]
--quiet, -q Suppress log messages from output [布尔]
--help, -h 显示帮助信息 [布尔]
--version, -v 显示版本号 [布尔]
示例:
json-server db.json
json-server file.js
json-server http://example.com/db.json
https://github.com/typicode/json-server