How Node js read the content of a file?
Normally NodeJs reads the content of a file in non-blocking, asynchronous way. Node Js uses its fs core API to deal with files. The easiest way to read the entire content of a file in nodeJs is with fs.readFile method. Below is sample code to read a file in NodeJs asynchronously and synchronously.
Reading a file in node asynchronously/ non-blocking
var fs = require('fs');
fs.readFile('DATA', 'utf8', function(err, contents) {
console.log(contents);
});
console.log('after calling readFile');
Reading a file in node asynchronously/blocking
var fs = require('fs');
var contents = fs.readFileSync('DATA', 'utf8');
console.log(contents);