0

I have created a nodejs gateway server, in which based on url prefix, it proxy using express-http-proxy:1.5.1 to many other microservices like this

this.router.all(Routes.ABC, httpProxy(baseUrl.XYZ, {
    parseReqBody: false,
  }));

now one of my ms has apis that holds form-data for that matter i need to set

parseReqBody : false

now if i do this, it will stop parsing body for other apis on the same ms.

I tried passing function to this parseReqBody so that based on Content-Type header i can set it to true or false.

But it does not accept function, it needs direct boolean value.

I want to make this prop false if there is form data otherwise it should parse the body.

And is there any way to work both?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

1 Answers1

0

this solved my problem

const isMultipartRequest = function (req: Request) {
  const contentTypeHeader = req.headers['content-type']
  return contentTypeHeader && contentTypeHeader.indexOf('multipart') > -1
}

const proxy = function (host: string) {
  return function (req: Request, res: Response, next: NextFunction) {
    let reqBodyEncoding
    let reqAsBuffer = false
    let parseReqBody = true

    if (isMultipartRequest(req)) {
      reqAsBuffer = true
      reqBodyEncoding = null
      parseReqBody = false
    }
    return default_proxy(host, {
      reqAsBuffer,
      reqBodyEncoding,
      parseReqBody
    })(req, res, next)
  }
}

app.all(/widget/, proxy('localhost:3002'))
Sunil Garg
  • 14,608
  • 25
  • 132
  • 189