Mock其实就是真实数据存在之前,即调试期间的代替品

mock.js

  1. const express = require('express')
  2. const Mock = require('mockjs')
  3. const app = express()
  4. app.get('/list', (req, res) => {
  5. const { limit = 20 } = req.query
  6. const result = Mock.mock({
  7. "code": 0,
  8. "message": "成功",
  9. [`data|${limit}`]: [{
  10. "id": "@id",
  11. "ip": "@ip",
  12. "name": "@cname",
  13. "userId": "@id",
  14. "stars|2": ['★'],
  15. "colors|2": { red: 'red', yellow: 'yellow', blue: 'blue' },
  16. "avatar": "@image()",
  17. "createAt": "@datetime"
  18. }]
  19. })
  20. res.json(result)
  21. })
  22. app.get('/title', (req, res) => {
  23. res.json({ title: '@cname' })
  24. })
  25. app.listen(3000)

运行服务

  1. node ./mock.js