From: book- "Nodejs in Action"
var server = http.createServer( function( req, res )
{
var url = parse( req.url);
var path = join( root, url.pathname);
var stream = fs.createReadStream( path);
stream.on( 'data', function( chunk)
{
res.write( chunk);
});
stream.on( 'end', function()
{
res.end();
});
});
Why is it a good idea to read and send data in chunks rather than use 'readFile' and pass everything at once?
Why should we prefer to use createReadStream
instead of readFile
to server static files in nodejs?