Instructions for the fs.appendFileSync method in node.js

  • 2020-05-07 19:12:40
  • OfStack

method description:

This method is similar to fs.appendFile () except that it operates synchronously, whereas fs.appendFile USES asynchrony.

syntax:


fs.appendFileSync(filename, data, [options])

Since this method belongs to fs module, fs module (var fs = require(" fs ") needs to be introduced before use.)

receive parameter:

1. filename {String}

2. data {String | Buffer}

3. options {Object}

          encoding {String | Null} default = 'utf8'

          mode {Number} default = 438 (aka 0666 in Octal)

          flag {String} default = 'a'

source code:


fs.appendFileSync = function(path, data, options) {
  if (!options) {
    options = { encoding: 'utf8', mode: 438 /*=0666*/, flag: 'a' };
  } else if (util.isString(options)) {
    options = { encoding: options, mode: 438, flag: 'a' };
  } else if (!util.isObject(options)) {
    throw new TypeError('Bad arguments');
  }
  if (!options.flag)
    options = util._extend({ flag: 'a' }, options);
  fs.writeFileSync(path, data, options);
};


Related articles: