From dffbd9047f6303f51228580b7f619a3509b7d6ed Mon Sep 17 00:00:00 2001 From: kongmoumou Date: Sat, 15 Feb 2020 18:48:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(mock-server):=20=E4=BF=AE=E5=A4=8D=20mock-s?= =?UTF-8?q?erver=20hot=20reload=20=E4=B8=8D=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/mock-server.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mock/mock-server.js b/mock/mock-server.js index 806fdacc..f07c55b0 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -54,6 +54,7 @@ module.exports = app => { extended: true })) + const initMockStartIndex = app._router.stack.length const mockRoutes = registerRoutes(app) var mockRoutesLength = mockRoutes.mockRoutesLength var mockStartIndex = mockRoutes.mockStartIndex @@ -66,7 +67,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() @@ -74,6 +75,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) {