NodeJS writes log file (Json)

I want to write logs to Json file

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!");
    }
});

      

But I'm rewriting magazines. How can I write somthing to a json file and not overwrite old logs?

+3


source to share


1 answer


You have to use fs.appendFile

Example:



fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('The "data to append" was appended to file!');
});

      

+12


source







All Articles