Jake
Jake

Reputation: 3028

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?

Upvotes: 4

Views: 4014

Answers (2)

Jonathan Ong
Jonathan Ong

Reputation: 20315

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

Upvotes: 12

user2012767
user2012767

Reputation: 41

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

Upvotes: 4

Related Questions