funkylover / mock-server Goto Github PK
View Code? Open in Web Editor NEWAn easy way to mock back-end api!
Home Page: https://github.com/funkyLover/mock-server
License: MIT License
An easy way to mock back-end api!
Home Page: https://github.com/funkyLover/mock-server
License: MIT License
勾选的set流程中的api, 在mock面板中有对应的api项, 此时要优化mock面板的显示提示用户因为会优先匹配中set面板中数据, 所以mock面板中的数据不会生效
在api列表中每一项的前面都有checkbox,如果该api只有一个mock,可以快速切换,如果有多个mock,则只能反选
api列表中每一项都有两个复制按钮,一个是复制完整url,一个是复制验证url(/$mock-api?api=${api}
)
使用中如果存在很多个api,要切换起来容易看花眼
增加筛选功能方便快速定位到具体api
现在如果直接在mock目录中安卓node依赖,会把node_modules也作为mock配置目录进行解析
对于 restful api 来说, 如 /api/user/:id
, mock需要支持以下各种情况
GET /api/user/:id
POST /api/user
PUT /api/user/:id
# ....
目前 mock-server 没有针对该场景做优化, 目前只能通过 data.js
导出 function, 并在其中判断 http method 后返回不同的数据
初步设想如下
/api/user/:id
- get.js
- post.js
- put.js
- ....
当命中 /api/user/:id
后, 会再去判断对应的 http method
, 执行不同的js文件
和data.js
一样, get/post/put
等js文件可以直接导出一个数据对象, 也可以导出一个方法
当没有匹配的时候, 如 GET /api/user/123
, 但是 /api/user/:id
目录下没有 get.js
, 就返回 405 method not allowed
RT
目前启动mock server之后,控制台不会有任务输出(包括错误)
出现问题后开发者只能靠查,比较不合理
如需要调试服务端渲染页面, 其中代理的代码几乎重复的
考虑将其封装成一个代理方法/模块, 内置到mock-server中去
如 前后端分离之更好的mock你的后端api#模板接口调试 & 微信登录支持 中的例子
const proxy = require('mock/proxy');
proxy({
url: 'https://api.target.com',
ip: 'xxx.xx.xx.xxx', // 目标服务器ip, 如果不指定ip, 则转发到域名`api.target.com`对应的服务器
pfx: fs.readFileSync('path/to/cert'),
passphrase: 'password of cert'
}).then(res => {
console.log(res); // 目标服务器的响应, 此时可以对其进行修改再返回至客户端
});
// mock/_set/流程1/api.target.com/api/login/data.js
const data = resolve('成功登陆'); // mock/api.target.com/api/成功登陆/data.js
module.export = data;
目前前端页面和交互都过于粗糙
后续参考下市面上其他admin boilerplate的样式改造一下
支持如/api_bin/:id/get_name
等restful格式支持
RT
// proxy.js
module.exports = {
'targer.api.com': 'http://ww.xx.yy.zz',
'targer.api.com/api': 'http://aa.bb.cc.dd',
'targer.api.com/*.js': '/path/to/local/fileOrFolder'
}
···
内置后端模板引擎解析器?
文件目录
|- api.mock.api
|- data
|- api
|- option1
|- data.js
应该只会有一个api mock选项api.mock.api/data/api
有两个api mock选项,api.mock.api/data/api
和api.mock.api
# 能返回mock的配置
api.target.com 127.0.0.1:8080
# 如果带上协议, 则不会正常返回mock数据
api.target.com http://127.0.0.1:8080
第一个问题:
关于 POST 的方式,文档没有看到相关说明,我试了 POST 对应的请求,结果是 404.
响应为:ENOENT: no such file or directory, stat .....
第二个问题:
#7 应用是支持了 Restful 风格的接口配置,希望补充下使用说明。
使用例子如下
const send = require('koa-send');
const static = '/path/to/static/path';
module.exports = async (ctx) => {
await send(ctx, ctx.path, {
root: static
});
return {
selfHandle: true
};
};
npm ERR! publish Failed PUT 401
npm ERR! code E401
npm ERR! You must be logged in to publish packages. : mock-server-local
npm ERR! A complete log of this run can be found in:
npm ERR! /home/travis/.npm/_logs/2019-03-21T12_31_02_715Z-debug.log
Error occurred while trying to proxy request to target server through https
简单来说就是批量切换的功能
现在哪怕使用新增的resolver
也不能解决这个问题
目前proxy属性会默认打开,即没有命中mock的api会直接代理到线上或指定ip
后续该特性会默认关闭,需要在proxy.js
指定打开
module.exports = {
'api.target.com': true,
'api.mock.com': ww.xxx.yy.zzz
};
RT
module.exports = {
mockChecked: {"api.target.com": 1},
setChecked: '[object Object]' // 这里应该是数字,表示用户关闭mock server时 set 面板的勾选值
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.