fs module - appendFile()

 fs module:

The Node.js file system module allows you to work with the file system on your computer.


appendFile(): 

Syntax:

fs.appendFile(path, data, options, callback);

appendFile() method creates a new file and inserts content inside the file. If the file exists, then it will only append the content with old content.


    var fs = require("fs");

    fs.appendFile("latest.txt", "Mozahedul Islam", function (err) {
      if (err) {
        throw err;
        console.log("Failed");
      }
    });


appendFileSync(): 

Synchronously append data to a file, creating the file if it does not yet exist.

Syntax:

fs.appendFileSync(path, data, options);

  
    try {
      fs.appendFileSync("message.txt", "data to append");
      console.log('The "data to append" was appended to file!');
    } catch (err) {
      /* Handle the error */
    }


Post a Comment

Previous Post Next Post