Read file in Node.js

Share:
Asynchronously reads the entire contents of a file.

 var fs = require('fs');  
 var path = require('path');  
 exports.testDir = path.dirname(__filename);  
 exports.fixturesDir = path.join(exports.testDir, 'fixtures');  
 exports.libDir = path.join(exports.testDir, '../lib');  
 exports.tmpDir = path.join(exports.testDir, 'tmp');  
 exports.PORT = +process.env.NODE_COMMON_PORT || 12346;  
 // Read File  
 fs.readFile(exports.tmpDir+'/world.csv', 'utf-8', function(err, content) {  
  if (err) {  
   got_error = true;  
  } else {  
   console.log('cat returned some content: ' + content);  
   console.log('this shouldn\'t happen as the file doesn\'t exist...');  
   //assert.equal(true, false);  
  }  
 });  

The readFile is passed two arguments (err, data), where data is the contents of the file.

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();

Ads