@@ -2,17 +2,21 @@ const chokidar = require('chokidar')
22const bodyParser = require ( 'body-parser' )
33const chalk = require ( 'chalk' )
44const path = require ( 'path' )
5+ const Mock = require ( 'mockjs' )
56
67const mockDir = path . join ( process . cwd ( ) , 'mock' )
78
89function registerRoutes ( app ) {
910 let mockLastIndex
1011 const { default : mocks } = require ( './index.js' )
11- for ( const mock of mocks ) {
12+ const mocksForServer = mocks . map ( route => {
13+ return responseFake ( route . url , route . type , route . response )
14+ } )
15+ for ( const mock of mocksForServer ) {
1216 app [ mock . type ] ( mock . url , mock . response )
1317 mockLastIndex = app . _router . stack . length
1418 }
15- const mockRoutesLength = Object . keys ( mocks ) . length
19+ const mockRoutesLength = Object . keys ( mocksForServer ) . length
1620 return {
1721 mockRoutesLength : mockRoutesLength ,
1822 mockStartIndex : mockLastIndex - mockRoutesLength
@@ -27,6 +31,18 @@ function unregisterRoutes() {
2731 } )
2832}
2933
34+ // for mock server
35+ const responseFake = ( url , type , respond ) => {
36+ return {
37+ url : new RegExp ( `${ process . env . VUE_APP_BASE_API } ${ url } ` ) ,
38+ type : type || 'get' ,
39+ response ( req , res ) {
40+ console . log ( 'request invoke:' + req . path )
41+ res . json ( Mock . mock ( respond instanceof Function ? respond ( req , res ) : respond ) )
42+ }
43+ }
44+ }
45+
3046module . exports = app => {
3147 // es6 polyfill
3248 require ( '@babel/register' )
0 commit comments