nodejs get file code example
Example 1: read file node
const fs = require("fs");
const content = fs.readFileSync("./my_file.txt");
console.log(content.toString());
Example 2: read a file nodejs
const fs = require('fs');
fs.readFile('my-file.txt', 'utf8', function(err, data) {
if (err) throw err;
console.log(data);
});
Example 3: nodejs readfile
const fs = require('fs');
fs.readFile('/Users/joe/test.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err);
return
}
console.log(data);
});
Example 4: javascript fs read
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});