Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I provide an input stream to node.js/express' send, or get its raw output stream?

I'm providing a route in my express app that provides the contents of a cloud file as a download. I have access to the cloud file's input stream, and I'd like to pipe that directly into the response's output stream. However, I'm using express, which doesn't seem to support an input stream.

I was hoping I could do this:

res.send (cloudInputStream);

but this doesn't work. Express' send takes a body or a buffer, but apparently not an input stream.

Since that's the case, what I'd like to do is set the headers using res.setHeader(), then get access to the raw output stream, and then:

cloudInputStream.pipe (responseOutputStream);

Is this possible?

Alternatively, I could turn read the input stream into a buffer, and provide that buffer to send. However, this reads the entire cloud file's contents into memory at one time, which I'd like to avoid.

Any thoughts?

like image 948
Jake Avatar asked Dec 05 '22 12:12

Jake


2 Answers

All you have to do is cloudInputStream.pipe(res) after setting your headers.

like image 82
Jonathan Ong Avatar answered Mar 30 '23 00:03

Jonathan Ong


You can do anything node can do. Use pipe for streams and res.set for header fields, or res.sendfile.

like image 31
user2012767 Avatar answered Mar 30 '23 01:03

user2012767