how to write to a file using fs in 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: how to right plain text format file in node js
var fs = require('fs')
var logger = fs.createWriteStream('log.txt', {
flags: 'a'
})
logger.write('some data')
logger.write('more data')
logger.write('and more')
Example 3: data not write in file node js
const fs = require('fs');
fs.readFile('file.txt', 'utf-8', (err, data) => {
if(err) {
throw err;
}
console.log(data);
});