PanJiaChen/vue-element-admin

responseFake中的写法疑问

samXxts opened this issue · 0 comments

Question(提问)

您好,以下疑问希望解答:
function registerRoutes(app) {
let mockLastIndex
const { mocks } = require('./index.js')
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)
mockLastIndex = app._router.stack.length
}
const mockRoutesLength = Object.keys(mocksForServer).length
return {
mockRoutesLength: mockRoutesLength,
mockStartIndex: mockLastIndex - mockRoutesLength
}
}

// user login
{
url: '/vue-element-admin/user/login',
type: 'post',
response: config => { // config 用于模拟请求的配置 他不是后面我们看到的req和res
console.log('config', config)
console.log('config.body', config.body)
const { username } = config.body
const token = tokens[username]

  // mock error
  if (!token) {
    return {
      code: 60204,
      message: 'Account and password are incorrect.'
    }
  } 

  return {
    code: 20000,
    data: token
  }
}

},

Steps to reproduce(问题复现步骤)

  1. res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond))
    在执行respond(req, res)函数时 为什么没有报错
  2. 难道是因为mock框架的原因 对req res config这三者之间产生了关联吗

Other relevant information(格外信息)

  • Your OS:
  • Node.js version:
  • vue-element-admin version: