postman能请求到后端接口,.但是前端发送请求,怎么请求,后端接口都没响应....
前端项目是vue-element-admin
报错信息:
HttpMessageNotReadableException: I/O error while reading input message; nested exception is org.apache.catalina.connector.ClientAbortException
修改mock-server.js文件
修改两部分内容,具体内容如下图,代码在下面
mock-server.js详细内容
const chokidar = require('chokidar')
const bodyParser = require('body-parser')
const chalk = require('chalk')
const path = require('path')
const Mock = require('mockjs')const mockDir = path.join(process.cwd(), 'mock')function registerRoutes(app) {let mockLastIndexconst { mocks } = require('./index.js')const mocksForServer = mocks.map(route => {return responseFake(route.url, route.type, route.response)})for (const mock of mocksForServer) {//post请求后端无响应调整app[mock.type](mock.url, bodyParser.json(),bodyParser.urlencoded({extended: true}),mock.response)// app[mock.type](mock.url, mock.response)mockLastIndex = app._router.stack.length}const mockRoutesLength = Object.keys(mocksForServer).lengthreturn {mockRoutesLength: mockRoutesLength,mockStartIndex: mockLastIndex - mockRoutesLength}
}function unregisterRoutes() {Object.keys(require.cache).forEach(i => {if (i.includes(mockDir)) {delete require.cache[require.resolve(i)]}})
}// 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 => {// parse app.body// https://expressjs.com/en/4x/api.html#req.body//post请求后端无响应调整
/* app.use(bodyParser.json())app.use(bodyParser.urlencoded({extended: true}))*/const mockRoutes = registerRoutes(app)var mockRoutesLength = mockRoutes.mockRoutesLengthvar mockStartIndex = mockRoutes.mockStartIndex// watch files, hot reload mock serverchokidar.watch(mockDir, {ignored: /mock-server/,ignoreInitial: true}).on('all', (event, path) => {if (event === 'change' || event === 'add') {try {// remove mock routes stackapp._router.stack.splice(mockStartIndex, mockRoutesLength)// clear routes cacheunregisterRoutes()const mockRoutes = registerRoutes(app)mockRoutesLength = mockRoutes.mockRoutesLengthmockStartIndex = mockRoutes.mockStartIndexconsole.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`))} catch (error) {console.log(chalk.redBright(error))}}})
}