2

I am using plain Node (v14 rn) and node-fetch. I need to pass on the incoming body on to a downstream fetch.

The body option on node-fetch's RequestInit accepts:

| "Request body. can be null, a string, a Buffer, a Blob, or a Node.js Readable stream"

To minimize memory consumption I would prefer to hand over the readable stream to fetch vs read the contents of the body into a string first.

How can I generate a ReadableStream from IncomingMessage so I could write code something like this (pseudo-code)

async function(req:IncomingMessage, res:ServerResponse) {
   // need the magic inside toReadableStream
   const readableStream = toReadableStream(req);
   const result = await fetch('http://example.com', {
      method: "POST",
      body: readableStream,
   });
   // handle the result and send response to client
}
ConfusedNoob
  • 9,826
  • 14
  • 64
  • 85

0 Answers0