diff --git a/mock/mock-server.js b/mock/mock-server.js index 8941ec0f..aba510f5 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -51,6 +51,7 @@ module.exports = app => { extended: true })) + const initMockStartIndex = app._router.stack.length const mockRoutes = registerRoutes(app) var mockRoutesLength = mockRoutes.mockRoutesLength var mockStartIndex = mockRoutes.mockStartIndex @@ -63,7 +64,7 @@ module.exports = app => { if (event === 'change' || event === 'add') { try { // remove mock routes stack - app._router.stack.splice(mockStartIndex, mockRoutesLength) + app._router.stack.splice(initMockStartIndex, mockRoutesLength) // clear routes cache unregisterRoutes() @@ -71,6 +72,8 @@ module.exports = app => { const mockRoutes = registerRoutes(app) mockRoutesLength = mockRoutes.mockRoutesLength mockStartIndex = mockRoutes.mockStartIndex + const newRoutes = app._router.stack.splice(mockStartIndex, mockRoutesLength) + app._router.stack.splice(initMockStartIndex, 0, ...newRoutes) console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`)) } catch (error) {