Skip to content

Instantly share code, notes, and snippets.

@OdinsHat
Last active August 29, 2015 14:14
Show Gist options
  • Save OdinsHat/49f856f0e451ae52cec9 to your computer and use it in GitHub Desktop.
Save OdinsHat/49f856f0e451ae52cec9 to your computer and use it in GitHub Desktop.
Simply reads a file and outputs it to console.log/stdout
var fs = require('fs');
var file = fs.createReadStream('origin.txt');
var destFile = fs.createWriteStream('destination.txt');
// Not surte why end: false required as it seems silly to close midway through copying?
file.pipe(destFile, {end: false});
file.on('end', function(){
destFile.end('Finished!');
});
/**
* Sends index.html to request.
*/
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, {end: false});
}).listen(8080);
var fs = require('fs');
var file = fs.createReadStream('fruits.txt');
file.pipe(process.stdout);
var fs = require('fs');
var file = new fs.createReadStream('fruits.txt');
file.on('readable', function(){
var chunk = null;
while(null !== (chunk = file.read())){
console.log(chunk.toString());
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment