How to append to a file in Node?

后端 未结 18 1577
庸人自扰
庸人自扰 2020-11-22 09:23

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.

fs.writeFile(\'log.txt\'         


        
18条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 10:08

    Besides appendFile, you can also pass a flag in writeFile to append data to an existing file.

    fs.writeFile('log.txt', 'Hello Node',  {'flag':'a'},  function(err) {
        if (err) {
            return console.error(err);
        }
    });
    

    By passing flag 'a', data will be appended at the end of the file.

提交回复
热议问题