mock-server.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. const chokidar = require('chokidar')
  11. const bodyParser = require('body-parser')
  12. const chalk = require('chalk')
  13. const path = require('path')
  14. const Mock = require('mockjs')
  15. const mockDir = path.join(process.cwd(), 'mock')
  16. function registerRoutes(app) {
  17. let mockLastIndex
  18. const { default: mocks } = require('./index.js')
  19. const mocksForServer = mocks.map(route => {
  20. return responseFake(route.url, route.type, route.response)
  21. })
  22. for (const mock of mocksForServer) {
  23. app[mock.type](mock.url, mock.response)
  24. mockLastIndex = app._router.stack.length
  25. }
  26. const mockRoutesLength = Object.keys(mocksForServer).length
  27. return {
  28. mockRoutesLength: mockRoutesLength,
  29. mockStartIndex: mockLastIndex - mockRoutesLength
  30. }
  31. }
  32. function unregisterRoutes() {
  33. Object.keys(require.cache).forEach(i => {
  34. if (i.includes(mockDir)) {
  35. delete require.cache[require.resolve(i)]
  36. }
  37. })
  38. }
  39. // for mock server
  40. const responseFake = (url, type, respond) => {
  41. return {
  42. url: new RegExp(`${process.env.VUE_APP_BASE_API}${url}`),
  43. type: type || 'get',
  44. response(req, res) {
  45. console.log('request invoke:' + req.path)
  46. res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
  47. }
  48. }
  49. }
  50. module.exports = app => {
  51. // es6 polyfill
  52. require('@babel/register')
  53. // parse app.body
  54. // https://expressjs.com/en/4x/api.html#req.body
  55. app.use(bodyParser.json())
  56. app.use(bodyParser.urlencoded({
  57. extended: true
  58. }))
  59. const mockRoutes = registerRoutes(app)
  60. var mockRoutesLength = mockRoutes.mockRoutesLength
  61. var mockStartIndex = mockRoutes.mockStartIndex
  62. // watch files, hot reload mock server
  63. chokidar.watch(mockDir, {
  64. ignored: /mock-server/,
  65. ignoreInitial: true
  66. }).on('all', (event, path) => {
  67. if (event === 'change' || event === 'add') {
  68. try {
  69. // remove mock routes stack
  70. app._router.stack.splice(mockStartIndex, mockRoutesLength)
  71. // clear routes cache
  72. unregisterRoutes()
  73. const mockRoutes = registerRoutes(app)
  74. mockRoutesLength = mockRoutes.mockRoutesLength
  75. mockStartIndex = mockRoutes.mockStartIndex
  76. console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))
  77. } catch (error) {
  78. console.log(chalk.redBright(error))
  79. }
  80. }
  81. })
  82. }