用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
[TOC] [sinonjs](https://sinonjs.org/) [https://mockoon.com/](https://mockoon.com/) mockapi.io [Don’t Waste Your Time Building Fake APIs in Code](https://stoplight.io/blog/dont-code-fake-apis/) [2019/frontend-first-api-mocking](https://www.unic.com/en/competencies/experts-blog/2019/frontend-first-api-mocking) [fastmock.site](https://fastmock.site/#/) [https://fake.rest/](https://fake.rest/) [YMFE/yapi](https://github.com/YMFE/yapi) # 生成API的文档 https://github.com/tripit/slate https://github.com/mapbox/docbox # Mock 平台 https://randomuser.me/ https://github.com/marak/faker.js http://jsonplaceholder.typicode.com/ ## 国内Mock 平台 https://easy-mock.com/ [Mock.js](http://mockjs.com/) https://apizza.net/pro/ https://nei.netease.com/ ## [Easy-Mock](http://www.easy-mock.com/) 感谢大搜车团队开放的模拟数据的在线Mock服务, ChandlerVer5 xw522101 # Mock 测试工具 ## https://apizza.net ## RAP2 http://rap2.taobao.org/ RAP2 **是一个Web接口管理工具,开源免费,接口自动化,MOCK数据自动生成,自动化测试,企业级管理。阿里妈妈MUX团队出品** [rap2快速入门](https://www.jianshu.com/p/22d21ec04cd0) # mock.js [justreq 与 mock.js 组合,快速打造高质量Mock Server](https://segmentfault.com/a/1190000008558079) 使用mock.js提供模拟数据 http://blog.csdn.net/cuo9958/article/details/53995182 https://github.com/retro/apitizer https://www.zhihu.com/question/35436669 http://sinonjs.org/ http://xwartz.xyz/blog/2016/09/mock-server/ # Nginx 代理 Nginx 的反向代理功能还是在现在的工作中使用了,才有所了解。 nginx.conf ``` server { listen 80; # 把所有 uri 以 /api 开头的转发到接口服务器 location /api { proxy_pass http://127.0.0.1:4000; } } ``` Nginx 了解的并不是很多,就不展开讨论了,还望人指点。 # 可以使用的 Mock Server 存在很多服务,可以使用,有以下推荐: [apiary](https://apiary.io/): 可以生成文档,但是非个人使用得付费。 [RAP2](https://github.com/thx/rap2-delos): 功能貌似很强大,阿里妈妈MUX团队出品。 [json-server](https://github.com/typicode/json-server): Node 编写,适合前端人员使用,但不能产生文档。 # 服务器端mock(php版) ```php <?php /** * Mock Json for Javascript * * @author soulteary * @date 2014-06-15 */ /** * 请求接口字段:字符集 */ define('charset', 'charset'); /** * 请求接口字段:回调函数名 */ define('callback', 'callback'); /** * 请求接口字段:跨域字段 */ define('crossDomain', 'cross-domain'); /** * 输出mock数据 * 如果存在mock.json文件,则数据从mock.js中获取 * * @return string */ function mockData() { if (file_exists('mock.json')) { $data = json_decode(file_get_contents('mock.json')); } else { $data = Array( 'code' => 200, 'desc' => 'Get the default data.', 'login' => true, 'data' => Array( 'name' => 'test api.' ) ); } return json_encode($data); } /** * 输出字符集,允许结果为gbk、gb2312、utf-8 * 如果非法或者未设置,输出utf-8 * * @return string */ function charset() { $ret = 'utf-8'; if (empty($_REQUEST[charset])) { return $ret; } else { $charset = strtolower($_REQUEST[charset]); if (in_array($charset, array('gbk', 'gb2312'), true)) { return $charset; } else { return $ret; } } } /** * 拼装json数据 * * @return string */ function jsonGenerator() { if (!empty($_REQUEST[callback])) { header('Content-Type: application/javascript; charset=' . charset()); return $_REQUEST[callback] . "(" . mockData() . ");"; } else { if (!empty($_REQUEST[crossDomain])) { header("Access-Control-Allow-Origin: *"); }; header('Content-type: application/json; charset=' . charset()); return mockData(); } } /** * 输出结果 */ die(jsonGenerator()); ``` # 调试 [Tencent/vConsole](https://github.com/Tencent/vConsole)