Node.js fs stream events

Description

Node.js fs stream events

#!/usr/bin/env node//  w  w  w.ja  v a 2  s  . com

// This experiment shows which file events are fired
// with a 'data' event handler.

"use strict";

var fs = require('fs');

var num = 0;
function logEvent(message) {
  console.log(++num + '. ' + message);
}

var file = process.argv[2];

if (file === undefined) {
  console.log("argument missing");
  process.exit(1);
}

console.log(
  "file '" + file + "' stream events with 'data' event handler:"
);

var stream = fs.createReadStream(file);

stream.on('open',     function () { logEvent('file open'); });
stream.on('end',      function () { logEvent('file end'); });
stream.on('close',    function () { logEvent('file close'); });
stream.on('data',     function () { logEvent('file data'); });
stream.on('readable', function () { logEvent('file readable'); });
stream.on('error',    function () { logEvent('file error'); });



PreviousNext

Related