1.安装json-server
npm install json-server
2.新建db.json
{
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
],
"profile": {
"name": "typicode"
}
}
3.安装mock
# 安装
npm install mockjs
4.新建mockData.js
const mock=require('mockjs') //引入mockjs
const random=mock.Random //引入random
module.exports=()=>{
let data={
user:[]
}
for (let i = 0; i < 10; i++) {
data.user.push({
id:random.guid(),
name:random.cname(),
age:random.integer(18,60),
sex:random.integer(0,1),
birth:random.date(),
})
}
return data
}
5.运行命令
json-server .\mockDatas.js
6.完成
运行完命令就会为我们生成接口
Resources
http://localhost:3000/user
Home
http://localhost:3000
可以访问接口查看数据
标签:title,random,server,json,虚拟,数据,id,mock From: https://blog.csdn.net/weixin_73098736/article/details/139867967