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?

1 Answer 1

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'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.