file write nodejs 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: writefile in node js
const fs = require('fs');
let writeStream = fs.createWriteStream('secret.txt');
writeStream.write('aef35ghhjdk74hja83ksnfjk888sfsf', 'base64');
writeStream.on('finish', () => {
console.log('wrote all data to file');
});
writeStream.end();
Example 3: 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 4: javascript write to file
<script>
const fs = require('fs')
let data = "Learning how to write in a file."
fs.writeFile('Output.txt', data, (err) => {
if (err) throw err;
})
</script>