@@ -2,17 +2,21 @@ const chokidar = require('chokidar')
2
2
const bodyParser = require ( 'body-parser' )
3
3
const chalk = require ( 'chalk' )
4
4
const path = require ( 'path' )
5
+ const Mock = require ( 'mockjs' )
5
6
6
7
const mockDir = path . join ( process . cwd ( ) , 'mock' )
7
8
8
9
function registerRoutes ( app ) {
9
10
let mockLastIndex
10
11
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 ) {
12
16
app [ mock . type ] ( mock . url , mock . response )
13
17
mockLastIndex = app . _router . stack . length
14
18
}
15
- const mockRoutesLength = Object . keys ( mocks ) . length
19
+ const mockRoutesLength = Object . keys ( mocksForServer ) . length
16
20
return {
17
21
mockRoutesLength : mockRoutesLength ,
18
22
mockStartIndex : mockLastIndex - mockRoutesLength
@@ -27,6 +31,18 @@ function unregisterRoutes() {
27
31
} )
28
32
}
29
33
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
+
30
46
module . exports = app => {
31
47
// es6 polyfill
32
48
require ( '@babel/register' )
0 commit comments