configuration installation
#全局安装
npm i @shymean/mock-server -g
#快速启动mock服务器
mock -p 9999 -f ./_mock.js
Parameter Description
port, server port number, default 7654, abbreviated -p
file, mock template file path, default ./_mock.js, abbreviated -f
template syntax
To use this tool, you only need to prepare a mock template file, and its grammar reference
@shymean/koa-mock` is used internally, which is a middleware mock template for quickly building a koa mock server
using mockjs syntax and extending related functions
// _mock.js
// 对应的rurl会被中间件拦截,并返回mock数据
// ANY localhost:9999/
Mock.mock('/', {
data: [],
msg: "hello mock",
"code|1-4": 1,
})
// 可以mock指定的请求方法
// POST localhost:9999/test
Mock.mock('/test', 'POST', {
data: [],
msg: "hello mock",
"code|1-4": 1,
})
// 扩展rtype,支持jsonp形式,使用param传入对应的回调名
// GET JSONP localhost:9999/test
Mock.mock('/test', {
type: 'jsonp',
param: 'callbackName'
},{
code: 0,
msg: 'hello from mock jsonp',
data: {
"id|1000-9999": 1,
}
})
// 默认回调名称 callback
Mock.mock("/test2", "jsonp", {
code: 0,
msg: "hello from mock jsonp2",
data: {
"id|1000-9999": 1,
}
});
Reference
https://blog.csdn.net/weixin_34138521/article/details/91442613