fs.writeFile node js code example
Example 1: writefile in node js
const fs = require('fs');
let lyrics = 'But still I\'m having memories of high speeds when the cops crashed\n' +
'As I laugh, pushin the gas while my Glocks blast\n' +
'We was young and we was dumb but we had heart';
fs.writeFile('2pac.txt', lyrics, (err) => {
if (err) throw err;
console.log('Lyric saved!');
});
Example 2: fs writefile node
const fs = require('fs')
const content = 'Some content!'
fs.writeFile('/Users/joe/test.txt', content, err => {
if (err) {
console.error(err)
return
}
})
Example 3: writefile in node js
const fs = require('fs');
fs.appendFile('empirestate.txt', '\nRight there up on Broadway', (err) => {
if (err) throw err;
console.log('The lyrics were updated!');
});
Example 4: fs readfile encoding
fs.readFile('/etc/passwd', 'utf8', callback);