@@ -2,15 +2,16 @@ const chokidar = require('chokidar')
22const bodyParser = require ( 'body-parser' )
33const chalk = require ( 'chalk' )
44const path = require ( 'path' )
5+ const express = require ( 'express' )
56
67const mockDir = path . join ( process . cwd ( ) , 'mock' )
78
8- function registerRoutes ( app ) {
9+ function registerRoutes ( router ) {
910 let mockLastIndex
1011 const { default : mocks } = require ( './index.js' )
1112 for ( const mock of mocks ) {
12- app [ mock . type ] ( mock . url , mock . response )
13- mockLastIndex = app . _router . stack . length
13+ router [ mock . type ] ( mock . url , mock . response )
14+ mockLastIndex = router . stack . length
1415 }
1516 const mockRoutesLength = Object . keys ( mocks ) . length
1617 return {
@@ -30,17 +31,19 @@ function unregisterRoutes() {
3031module . exports = app => {
3132 // es6 polyfill
3233 require ( '@babel/register' )
34+ const router = express . Router ( )
3335
3436 // parse app.body
3537 // https://expressjs.com/en/4x/api.html#req.body
36- app . use ( bodyParser . json ( ) )
37- app . use ( bodyParser . urlencoded ( {
38+ router . use ( bodyParser . json ( ) )
39+ router . use ( bodyParser . urlencoded ( {
3840 extended : true
3941 } ) )
4042
41- const mockRoutes = registerRoutes ( app )
43+ const mockRoutes = registerRoutes ( router )
4244 var mockRoutesLength = mockRoutes . mockRoutesLength
4345 var mockStartIndex = mockRoutes . mockStartIndex
46+ app . use ( / ^ \/ m o c k / , router )
4447
4548 // watch files, hot reload mock server
4649 chokidar . watch ( mockDir , {
@@ -50,12 +53,12 @@ module.exports = app => {
5053 if ( event === 'change' || event === 'add' ) {
5154 try {
5255 // remove mock routes stack
53- app . _router . stack . splice ( mockStartIndex , mockRoutesLength )
56+ router . stack . splice ( mockStartIndex , mockRoutesLength )
5457
5558 // clear routes cache
5659 unregisterRoutes ( )
5760
58- const mockRoutes = registerRoutes ( app )
61+ const mockRoutes = registerRoutes ( router )
5962 mockRoutesLength = mockRoutes . mockRoutesLength
6063 mockStartIndex = mockRoutes . mockStartIndex
6164
0 commit comments