Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
W
web-monitor
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
web-monitor
Commits
d0518bce
Commit
d0518bce
authored
Jun 04, 2020
by
潘嘉晨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
perf[Mock]: set responseFake to mock-server.js
https://github.com/PanJiaChen/vue-element-admin/pull/2966
parent
95c7bf5d
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
17 deletions
+19
-17
index.js
mock/index.js
+1
-15
mock-server.js
mock/mock-server.js
+18
-2
No files found.
mock/index.js
View file @
d0518bce
...
@@ -50,18 +50,4 @@ export function mockXHR() {
...
@@ -50,18 +50,4 @@ export function mockXHR() {
}
}
}
}
// for mock server
export
default
mocks
const
responseFake
=
(
url
,
type
,
respond
)
=>
{
return
{
url
:
new
RegExp
(
`
${
process
.
env
.
VUE_APP_BASE_API
}${
url
}
`
),
type
:
type
||
'get'
,
response
(
req
,
res
)
{
console
.
log
(
'request invoke:'
+
req
.
path
)
res
.
json
(
Mock
.
mock
(
respond
instanceof
Function
?
respond
(
req
,
res
)
:
respond
))
}
}
}
export
default
mocks
.
map
(
route
=>
{
return
responseFake
(
route
.
url
,
route
.
type
,
route
.
response
)
})
mock/mock-server.js
View file @
d0518bce
...
@@ -2,17 +2,21 @@ const chokidar = require('chokidar')
...
@@ -2,17 +2,21 @@ const chokidar = require('chokidar')
const
bodyParser
=
require
(
'body-parser'
)
const
bodyParser
=
require
(
'body-parser'
)
const
chalk
=
require
(
'chalk'
)
const
chalk
=
require
(
'chalk'
)
const
path
=
require
(
'path'
)
const
path
=
require
(
'path'
)
const
Mock
=
require
(
'mockjs'
)
const
mockDir
=
path
.
join
(
process
.
cwd
(),
'mock'
)
const
mockDir
=
path
.
join
(
process
.
cwd
(),
'mock'
)
function
registerRoutes
(
app
)
{
function
registerRoutes
(
app
)
{
let
mockLastIndex
let
mockLastIndex
const
{
default
:
mocks
}
=
require
(
'./index.js'
)
const
{
default
:
mocks
}
=
require
(
'./index.js'
)
for
(
const
mock
of
mocks
)
{
const
mocksForServer
=
mocks
.
map
(
route
=>
{
return
responseFake
(
route
.
url
,
route
.
type
,
route
.
response
)
})
for
(
const
mock
of
mocksForServer
)
{
app
[
mock
.
type
](
mock
.
url
,
mock
.
response
)
app
[
mock
.
type
](
mock
.
url
,
mock
.
response
)
mockLastIndex
=
app
.
_router
.
stack
.
length
mockLastIndex
=
app
.
_router
.
stack
.
length
}
}
const
mockRoutesLength
=
Object
.
keys
(
mocks
).
length
const
mockRoutesLength
=
Object
.
keys
(
mocks
ForServer
).
length
return
{
return
{
mockRoutesLength
:
mockRoutesLength
,
mockRoutesLength
:
mockRoutesLength
,
mockStartIndex
:
mockLastIndex
-
mockRoutesLength
mockStartIndex
:
mockLastIndex
-
mockRoutesLength
...
@@ -27,6 +31,18 @@ function unregisterRoutes() {
...
@@ -27,6 +31,18 @@ function unregisterRoutes() {
})
})
}
}
// for mock server
const
responseFake
=
(
url
,
type
,
respond
)
=>
{
return
{
url
:
new
RegExp
(
`
${
process
.
env
.
VUE_APP_BASE_API
}${
url
}
`
),
type
:
type
||
'get'
,
response
(
req
,
res
)
{
console
.
log
(
'request invoke:'
+
req
.
path
)
res
.
json
(
Mock
.
mock
(
respond
instanceof
Function
?
respond
(
req
,
res
)
:
respond
))
}
}
}
module
.
exports
=
app
=>
{
module
.
exports
=
app
=>
{
// es6 polyfill
// es6 polyfill
require
(
'@babel/register'
)
require
(
'@babel/register'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment