node.js append to file code example
Example 1: How to append to a file in Node?
// Asynchronously:
const fs = require('fs');
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('Saved!');
});
// Synchronously:
const fs = require('fs');
fs.appendFileSync('message.txt', 'data to append');
Example 2: javascript fs read
fs.readFile('/etc/passwd', (err, data) => {
if (err) throw err;
console.log(data);
});