fs read file async code example
Example 1: fs readfile not working
const content = fs.readFileSync('./Index.html', 'utf8');
console.log(content);
Example 2: how to export fs.readFile
var fs = require('fs');
module.exports = {
parseFile: function(file_path, callback) {
fs.readFile(file_path.toString(), 'utf-8', function(err, data) {
if (err) return callback(err);
callback(null, data);
});
}
}
exports.parseFile = function(file_path, callback) {
fs.readFile(file_path.toString(), 'utf-8', callback);
}
Example 3: readfile async nodejs
const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
function getStuff() {
return readFile('test');
}
getStuff().then(data => {
console.log(data);
})