mock

本地搭建

  • nodejs 8.9.4
  • Redis 4.0.2.2 (00000000/0) 64 bit
  • Mongodb 3.4.19 2008R2Plus SSL(64bit)

安装

官方教程 https://github.com/easy-mock/easy-mock/blob/dev/README.zh-CN.md
0.下载并安装nodejs
1.下载redis http://www.runoob.com/redis/redis-install.html
2.安装mongodb http://www.runoob.com/mongodb/mongodb-window-install.html

D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe --config "D:\Program Files\MongoDB\Server\3.4\data\mongod.cfg" --install
3.安装easy-mock
npm install

配置

:修改配置在easy-mock\config\ ,复制local一份命名为local.json, 注意下面的三个服务端口

"port": 7300,
"host": "xxx.xx", //本地ip
"db": "mongodb://localhost:27017/easy-mock",
"redis": {
"keyPrefix": "[Easy Mock]",
"port": 6379,
"host": "localhost",
"password": "",
"db": 0
},


启动(保存.bat文件)

1.启动redis

cd /d D:\software\redis
redis-server.exe redis.windows.conf

2.启动mongodb服务

net start MongoBD

3.启动easy-mock:

cd /d D:\software\easy-mock
npm run dev

启动服务,访问本机访问 http://127.0.0.1:7300


示例

1.随机数、占位符

{
"success": true,
"data": {
"projects|3-10": [{
"name": "演示用",
"url": "@url",
"email": "@email",
"address": "@county(true)",
"string|1-10": "★",
"number|1-100": 100,
"boolean|1-2": true,
"object|2": {
"310000": "上海市",
"320000": "江苏省",
"330000": "浙江省"
}
}]
}
}

2.获取post请求的数组,遍历返回随机数

{
"success": true,
sku_details: function({
_req,
Mock
}) {
var myArray = _req.body.sku_details
var arr = new Array()
var i
for (var i in myArray) {
myArray[i].batch_no = Math.random().toString(32).substr(2) + Math.random().toString(32).substr(2)
arr.push(myArray[i])
}
return arr
}
}

post请求:
{
"sku_details": [{
"sku_id": "34922-68a",
"batch_no": null,
},
{
"sku_id": "34922-",
"batch_no": null,
}]
}
响应:
{
"success": true,
"sku_details": [
{
"sku_id": "34922-68a3e5516d7a7dc21fbe0e7ee13bfc1c",
"batch_no": "msf4frkiu9g5nn4fvhp4s8",
},
{
"sku_id": "34922-68a3e551",
"batch_no": "hivapgq0m984h1mb9c043o",
}
]
}

你可能感兴趣的:(mock)