我想将日志写入Json文件
newEntry = "User: " + lastUsername + " Time: "+now+ " Door: "+IOSDoor;
lastUserOpenClose += newEntry;
jsonString = JSON.stringify(lastUserOpenClose);
fs.writeFile("lastUserOpenClose.json", lastUserOpenClose, function(err) {
if(err) {
console.log(err);
} else {
console.log("Server-Log: The file was saved!");
}
});
但我正在覆盖日志。 如何将somthing写入json文件而不覆盖旧日志?
答案 0 :(得分:12)
您应该使用fs.appendFile
示例:
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});