我想把日志写成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!");
}
});
但我正在覆盖日志。 如何将内容写入 json 文件并且不覆盖旧日志?
最佳答案
你应该使用 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!');
});
关于json - NodeJS 写入日志文件(Json),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25305622/