From 1dfb9886b3c6445313a60108e7bd005032736e7a Mon Sep 17 00:00:00 2001 From: liyonglei <573044965@qq.com> Date: Mon, 9 Mar 2020 21:36:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3https://github.com/PanJiaChen?= =?UTF-8?q?/vue-element-admin/issues/3020=E9=97=AE=E9=A2=98,=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=BA=8C=E8=BF=9B=E5=88=B6=E8=AF=B7?= =?UTF-8?q?=E6=B1=82mock=E8=8E=B7=E5=8F=96=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6multer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mock/mock-server.js | 16 +++++++--------- package.json | 1 + 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/mock/mock-server.js b/mock/mock-server.js index 806fdacc..cc33fd9d 100644 --- a/mock/mock-server.js +++ b/mock/mock-server.js @@ -1,11 +1,12 @@ const chokidar = require('chokidar') const bodyParser = require('body-parser') +const multer = require('multer') const chalk = require('chalk') const path = require('path') const Mock = require('mockjs') const mockDir = path.join(process.cwd(), 'mock') - +const upload = multer() function registerRoutes(app) { let mockLastIndex const { default: mocks } = require('./index.js') @@ -13,7 +14,11 @@ function registerRoutes(app) { return responseFake(route.url, route.type, route.response) }) for (const mock of mocksForServer) { - app[mock.type](mock.url, mock.response) + // parse app.body + // https://expressjs.com/en/4x/api.html#req.body + app[mock.type](mock.url, bodyParser.json(), bodyParser.urlencoded({ + extended: true + }), upload.any(), mock.response) mockLastIndex = app._router.stack.length } const mockRoutesLength = Object.keys(mocksForServer).length @@ -47,13 +52,6 @@ module.exports = app => { // es6 polyfill require('@babel/register') - // parse app.body - // https://expressjs.com/en/4x/api.html#req.body - app.use(bodyParser.json()) - app.use(bodyParser.urlencoded({ - extended: true - })) - const mockRoutes = registerRoutes(app) var mockRoutesLength = mockRoutes.mockRoutesLength var mockStartIndex = mockRoutes.mockStartIndex diff --git a/package.json b/package.json index 448d1ec3..430660c1 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,7 @@ "husky": "1.3.1", "lint-staged": "8.1.5", "mockjs": "1.0.1-beta3", + "multer": "^1.4.2", "node-sass": "^4.9.0", "plop": "2.3.0", "runjs": "^4.3.2",