@iitrend/mock-server
v1.6.5
Published
IITrend Mock Server.
Downloads
6
Readme
mock-server
接口模拟数据-服务器
使用/启动方法
const MockServer = require('@iitrend/mock-server')
// 建议在业务代码编译完成之前启动
new MockServer({
mockPort: 8810
})
核心 API
new MockServer(config)
启动一个 Server 监听接受请求,并交由 @iitrend/mock-server/core
处理并生成模拟数据。
参数
config
// 以下为默认参数
{
// 自动启动服务
autoStart: true,
// server
mockHost: 'localhost',
mockPort: 8810,
// 配置非json文件指向(匹配中的使用使用js加载方式)
mockData: {
'static/static-config': true
},
// 默认为项目根目录下的 => 'mock/src/’
mockDataPath: path.join(process.cwd(), 'mock/src/'),
// 接口请求延迟(取区间随机值)
mockDelays: [0, 300],
// express app headers
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': '*',
'Content-Type': 'application/json;charset=utf-8'
}
// Mock.Random.extend
// mockExtends: {},
// http://localhost:8810/WHActs/Api/index.php/Client/Home/showIndex => 'Client/Home/showIndex'
// parseMockFile: (url = '') => url.replace(/\?\S*/ig, '').split('/').slice(-3).join('/')
}
目录结构
mock/src/**/*.json
# 例如请求接口:Client/Home/showIndex
文件及目录:mock/src/Client/Home/showIndex.json
{
"user": "@first()",
"nickname": "@cname()",
"realname": "@cname()",
"firm_name": "广州@ctitle(2,5)有限公司",
"dept_name": "@ctitle(2,5)部",
"sex|1-2": 1,
"birthday": "@timestamp()",
"intro": "@ctitle(5,10)",
"area": "@province()",
"target_step_list": [2000, 4000, 6000, 8000, 10000, 12000],
"target_step_n|+1": [2000, 4000, 6000, 8000, 10000, 12000],
"step_n|10000-20000": 10000,
"step_today|10000-20000": 10000,
"cal_n|10000-20000": 10000,
"cal_today|1000-2000": 10000,
"icon": "@imageFix(50x50)",
"firm_icon": "@imageFix(200x100)",
"phone": "@phone()",
"addr": {
"id|+1": 1,
"name": "@cname()",
"addr": "@province()@city()@county()@ctitle(1,3)街@integer(1,2000)号",
"phone": "@phone()"
},
"weight|50000-100000": 70000,
"height|100-240": 1
}