vbenjs / vite-plugin-mock

A mock plugin for vite.use mockjs.
MIT License
610 stars 96 forks source link

rawResponse能否给一个method为get的例子? #136

Open xiebruce opened 4 months ago

xiebruce commented 4 months ago

rawResponse能否给一个method为get的例子?我需要从req中获取get请求传过来的参数,http:127.0.0.1:5173/mock/admin/login?a=1&b=2,我要获取到这个a和b的值,现有的例子是post的

{
    url: '/api/text',
    method: 'post',
    rawResponse: async (req, res) => {
      let reqbody = ''
      await new Promise((resolve) => {
        req.on('data', (chunk) => {
          reqbody += chunk
        })
        req.on('end', () => resolve(undefined))
      })
      res.setHeader('Content-Type', 'text/plain')
      res.statusCode = 200
      res.end(`hello, ${reqbody}`)
    },
  },
XiaopingHu commented 3 months ago

这个看源码就知道怎么处理了,导入一个url模块,用url.parse(req.url, true)就能获得 或者使用response选项,在方法中,用this.req或this.res就能获取到req和res,this.parseJson()就能获取到body参数