[Node.js] Level 3 new. Steam
File Read Stream
Lets use the fs
module to read a file and log its contents to the console.
Use the fs
module to create a Readable
stream for fruits.txt
. Store the new stream in a variable called file
.
fs.createReadStream('fruits.txt');
Next, listen to the readable
event on the newly created stream and give it a callback.
file.on('readable', function(){});
Inside the callback, read the data chunks from the stream and print them to the console using console.log()
- you might want to use a while
loop to do this. Don't forget to call toString()
on the data before printing it.
file.on('readable', function(){ while(null !== (chunk = file.read())){ console.log(chunk.toString()); } });
var fs = require('fs'); var file = fs.createReadStream('fruits.txt'); file.on('readable', function(){ while(null !== (chunk = file.read())){ console.log(chunk.toString()); } });
File Piping
Instead of manually listening for the 'readable'
event on theReadable
stream, let's use pipe
to read from the stream and write directly to process.stdout
.
Start by removing the code for the readable
handler.
Call file.pipe()
, passing it the stream to write to.
var file = fs.createReadStream('fruits.txt'); file.pipe(process.stdout);
Read More: http://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
For example, emulating the Unix cat command:
process.stdin.pipe(process.stdout);
var fs = require('fs'); var file = fs.createReadStream('fruits.txt'); file.pipe(process.stdout);
Fixing Pipe
The following code will throw an error because pipe automatically closed our writable stream.
You'll need to consult the pipe documentation to figure out the option which keeps the Write stream open and dispatches the end
event.
By default end()
is called on the destination when the source stream emits end
, so that destination
is no longer writable. Pass{ end: false }
as options
to keep the destination stream open.
file.pipe(destFile, { end: false });
var fs = require('fs'); var file = fs.createReadStream('origin.txt'); var destFile = fs.createWriteStream('destination.txt'); file.pipe(destFile, { end: false }); file.on('end', function(){ destFile.end('Finished!'); });
Download Server
Let's create an HTTP server that will serve index.html
.
Use pipe()
to send index.html
to the response
.
var fs = require('fs'); var http = require('http'); http.createServer(function(request, response) { response.writeHead(200, {'Content-Type': 'text/html'}); var file = fs.createReadStream('index.html'); file.pipe(response); }).listen(8080);