refactor(mock): mak mock api compatible with both web-view and webpack server
1. 把 Mockjs 功能移到 server 端中间件,同时也兼容前端直接劫持 XHR 2. dev 环境下默认作为 express 中间件通过 webpack server 提供 mock api 3. prod 构建时,默认在前端用 Mockjs 劫持 XHR benefits: - dev 开发调试时能直接看到 XHR 请求,方便调试网络,能和后端对接联调 - 避开在开发时因为 Mockjs 引起的网络 bug - prod 构建时劫持 XHR,保证本项目的 Github Pages preview 能正常显示 (逻辑和 error-log 一样) - 前后台使用的 mock 是同一份代码,不会增加维护负担 ref: [#562](https://github.com/PanJiaChen/vue-element-admin/issues/562#issuecomment-378116233)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
'use strict'
|
||||
require('@babel/register')
|
||||
const path = require('path')
|
||||
const bodyParser = require('body-parser')
|
||||
|
||||
function resolve(dir) {
|
||||
return path.join(__dirname, dir)
|
||||
@@ -8,7 +9,28 @@ function resolve(dir) {
|
||||
|
||||
module.exports = {
|
||||
devServer: {
|
||||
open: true
|
||||
open: true,
|
||||
proxy: {
|
||||
'/api': {
|
||||
target: 'http://localhost:8080/mock',
|
||||
changeOrigin: true,
|
||||
pathRewrite: {
|
||||
'^/api': ''
|
||||
}
|
||||
}
|
||||
},
|
||||
after(app) {
|
||||
// parse app.body
|
||||
// http://expressjs.com/en/4x/api.html#req.body
|
||||
app.use(bodyParser.json())
|
||||
app.use(bodyParser.urlencoded({ extended: true }))
|
||||
|
||||
// import ES2015 module from common.js module
|
||||
const { default: mocks } = require('./mock')
|
||||
for (const mock of mocks) {
|
||||
app.all(mock.route, mock.response)
|
||||
}
|
||||
}
|
||||
},
|
||||
configureWebpack: {
|
||||
resolve: {
|
||||
|
||||
Reference in New Issue
Block a user