This commit is contained in:
Pan 2019-04-08 10:36:30 +08:00
parent 519847e865
commit 8aa8e357d9
2 changed files with 9 additions and 9 deletions

View File

@ -2,7 +2,7 @@ import Mock from 'mockjs'
import mocks from './mocks'
import { param2Obj } from '../src/utils'
const MOCK_API_BASE = '/mock'
export const MOCK_API_BASE = '/mock'
export function mockXHR() {
// 修复在使用 MockJS 情况下,设置 withCredentials = true且未被拦截的跨域请求丢失 Cookies 的问题

View File

@ -3,20 +3,20 @@ const bodyParser = require('body-parser')
const chalk = require('chalk')
function registerRoutes(app) {
const { default: mocks } = require('./index.js')
const { default: mocks, MOCK_API_BASE } = require('./index.js')
for (const mock of mocks) {
app[mock.type](mock.url, mock.response)
}
return {
mockRoutesLength: Object.keys(mocks).length,
caches: require.cache
MOCK_API_BASE: MOCK_API_BASE
}
}
function unregisterRoutes(caches) {
Object.keys(caches).forEach(i => {
if (i.includes('/mock')) {
delete caches[require.resolve(i)]
function unregisterRoutes(MOCK_API_BASE) {
Object.keys(require.cache).forEach(i => {
if (i.includes(MOCK_API_BASE)) {
delete require.cache[require.resolve(i)]
}
})
}
@ -54,7 +54,7 @@ module.exports = app => {
extended: true
}))
const { mockRoutesLength, caches } = registerRoutes(app)
const { mockRoutesLength, MOCK_API_BASE } = registerRoutes(app)
// watch files, hot reload mock server
chokidar.watch(('./mock'), {
@ -70,7 +70,7 @@ module.exports = app => {
app._router.stack.splice(index, mockRoutesLength)
// clear routes cache
unregisterRoutes(caches)
unregisterRoutes(MOCK_API_BASE)
registerRoutes(app)