How to append to a file in Node?

后端 未结 18 1176
庸人自扰
庸人自扰 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:22

    Try to use flags: 'a' to append data to a file

     var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
      stream.once('open', function(fd) {
        stream.write(msg+"\r\n");
      });
    

提交回复
热议问题