Mock其实就是真实数据存在之前,即调试期间的代替品
mock.js
const express = require('express')
const Mock = require('mockjs')
const app = express()
app.get('/list', (req, res) => {
const { limit = 20 } = req.query
const result = Mock.mock({
"code": 0,
"message": "成功",
[`data|${limit}`]: [{
"id": "@id",
"ip": "@ip",
"name": "@cname",
"userId": "@id",
"stars|2": ['★'],
"colors|2": { red: 'red', yellow: 'yellow', blue: 'blue' },
"avatar": "@image()",
"createAt": "@datetime"
}]
})
res.json(result)
})
app.get('/title', (req, res) => {
res.json({ title: '@cname' })
})
app.listen(3000)
运行服务
node ./mock.js