Skip to content

Instantly share code, notes, and snippets.

@mikegerwitz
Created March 3, 2011 23:24
Show Gist options
  • Save mikegerwitz/853846 to your computer and use it in GitHub Desktop.
Save mikegerwitz/853846 to your computer and use it in GitHub Desktop.
var fs = require( 'fs' ),
// size in bytes
CHUNK_SIZE = 50,
file = fs.open( 'file', 'r', '0666', function( err, fd )
{
if ( err )
{
console.log( err );
return;
}
doRead( fd, function()
{
console.log( 'File has been read.' );
});
});
function doRead( fd, callback )
{
var data = new Buffer( CHUNK_SIZE );
fs.read( fd, data, 0, CHUNK_SIZE, null, function( err, bytesRead )
{
if ( err )
{
// do something
}
// your HTTP stuff here. Data is in the 'data' buffer
console.log( data.toString().substr( 0, bytesRead ) );
// recursively read
if ( bytesRead > 0 )
{
doRead( fd, callback );
}
else
{
callback();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment