HTTP JSON接口模拟工具Interfake快速入门教程
作者:简单点的笨演员
这篇文章主要为大家介绍了HTTP JSON接口模拟工具Interfake快速入门教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
一、简介
Interfake能简便地创建虚假的HTTP API,只需简单几行代码就可以创建模拟JSON接口(使用命令行方式也可以不写代码),不需要安装web服务器。Interfake是NodeJs开发的,是一款开源Mock Server工具。
二、以命令行方式使用 interfake
interfake 是NodeJs开发的,支持三种使用方式:JS代码方式、JSON配置方式和HTTP meta-API方式,本文选择不用写代码的、易读的JSON配置方式。
JSON配置方式是按照interfake的配置格式将请求接口和返回内容保存到一个JSON文件中,再以命令行的形式启动interfake。
- 安装interfake
npm install request -g npm install interfake -g
- 创建一个JSON配置文件
adventuretime.json
[
{
"request": {
"url": "/hello",
"method": "get"
},
"response": {
"code": 200,
"delay": 100,
"body": {
"say": "hello world!"
}
}
},
{
"request": {
"url": "/whattimeisit",
"method": "get"
},
"response": {
"code": 200,
"delay": 100,
"body": {
"theTime": "Adventure Time!",
"starring": [
"Finn",
"Jake"
],
"location": "ooo"
}
}
}
]- 在命令行中启动interfake
interfake -p 3001 --file ./adventuretime.json
- 用浏览器打开测试一下
http://localhost:3001/hello
三、已知问题
很方便的命令行参数 --watch 用来监听JSON配置改动,经过测试,JSON改动后,interfake 崩溃退出。Interfake已经1年多没有更新了,估计作者不会解决这个问题了,没有办法了,先将就着用吧。

以上就是HTTP JSON接口模拟工具Interfake快速入门教程的详细内容,更多关于HTTP JSON接口模拟Interfake的资料请关注脚本之家其它相关文章!
