nodejs create a text file code example
Example 1: how to right plain text format file in node js
var fs = require('fs')
fs.appendFile('log.txt', 'new data', function (err) {
if (err) {
} else {
}
})
Example 2: writefile in node js
const fs = require('fs');
let path = 'ghetto_gospel.txt';
let buffer = new Buffer('Those who wish to follow me\nI welcome with my hands\nAnd the red sun sinks at last');
fs.open(path, 'w', function(err, fd) {
if (err) {
throw 'could not open file: ' + err;
}
fs.write(fd, buffer, 0, buffer.length, null, function(err) {
if (err) throw 'error writing file: ' + err;
fs.close(fd, function() {
console.log('wrote the file successfully');
});
});
});