This article mainly introduces node. fs. the appendFile method is described in this article. appendFile method description, syntax, receive parameters, use instances, and implement source code. For more information, see
Method description:
This method inserts data into a file asynchronously. if the file does not exist, it is automatically created. Data can be any string or cache.
Syntax:
The code is as follows:
Fs. appendFile (filename, data, [options], callback)
Because this method belongs to the fs module, we need to introduce the fs module (var fs = require ("fs") before use "))
Receiving parameters:
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'
4. callback {Function}
Example:
The code is as follows:
Var fs = require ("fs ");
Fs.appendFile('message.txt ', 'data to append', function (err ){
If (err) throw err;
Console. log (The "data to append" was appended to file! ');
});
Source code:
The code is as follows:
Fs. appendFile = function (path, data, options, callback _){
Var callback = maybeCallback (arguments [arguments. length-1]);
If (util. isFunction (options) |! 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. writeFile (path, data, options, callback );
};