const chokidar = require('chokidar') const bodyParser = require('body-parser') const chalk = require('chalk') const path = require('path') const Mock = require('mockjs') const express = require('express') const mockDir = path.join(process.cwd(), 'mock') function unregisterRoutes() { Object.keys(require.cache).forEach(i => { if (i.includes(mockDir)) { delete require.cache[require.resolve(i)] } }) } // for mock server const responseFake = (url, type, respond) => { return { url: new RegExp(url), type: type || 'get', response(req, res) { console.log('request invoke:' + req.path) res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond)) } } } let mockRouter function setupMocks(app) { mockRouter = new express.Router() const { mocks } = require('./index.js') const mocksForServer = mocks.map(route => { return responseFake(route.url, route.type, route.response) }) for (const mock of mocksForServer) { mockRouter[mock.type](mock.url, mock.response) } } module.exports = app => { // parse app.body // https://expressjs.com/en/4x/api.html#req.body app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true })) setupMocks(app) app.use(process.env.VUE_APP_BASE_API, function replacableRouter(req, res, next) { mockRouter(req, res, next) }) // watch files, hot reload mock server chokidar.watch(mockDir, { ignored: /mock-server/, ignoreInitial: true }).on('all', (event, path) => { if (event === 'change' || event === 'add') { try { unregisterRoutes() setupMocks(app) console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`)) } catch (error) { console.log(chalk.redBright(error)) } } }) }