Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Open
xiebruce opened this issue May 5, 2024 · 1 comment
Open

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

xiebruce opened this issue May 5, 2024 · 1 comment

Comments

@xiebruce
Copy link

xiebruce commented May 5, 2024

rawResponse能否给一个method为get的例子?我需要从req中获取get请求传过来的参数,http:127.0.0.1:5173/mock/admin/#?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
Copy link

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

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants