Pipe Multiple Files To One Response
I'm trying to write two files and some more text to a response. The code below does however only return the first file and the 'Thats all!' text. var http = require('http'), ut
Solution 1:
What you need is a Duplex stream, which will pass the data from one stream to another.
stream.pipe(duplexStream).pipe(stream2).pipe(res);
In the example above the first pipe will write all data from stream
to duplexStream
then there will be data written from the duplexStream
to the stream2
and finally stream2
will write your data to the res
writable stream.
This is an example of one possible implementation of the write method.
DuplexStream.write = function(chunk, encoding, done) {
this.push(chunk);
}
Solution 2:
you can use stream-stream It's a pretty basic node module (using no dependencies)
var ss = require('stream-stream');
var fs = require('fs');
var files = ['one.html', 'two.html'];
var stream = ss();
files.forEach(function(f) {
stream.write(fs.createReadStream(f));
});
stream.end();
res.writeHead(200, {'Content-Type' : 'text/plain'});
stream.pipe(res, { end:false});
Post a Comment for "Pipe Multiple Files To One Response"