lundi 30 mars 2015

Nodejs Writing to external JSON file

I am trying to write a JSOn object to an external JSON file. something like logleak.json. This is a log file which outputs everything to a JSON file. The problem is its not going like a JSON.




fs.open("logleak.json","a", function()
{
fs.exists('logleak.json',function(exist)
{
if(exist)
{
console.log("File Exists");
fs.readFile("logleak.json",function(err,data)
{

if(err) throw err;
log("=====================================================================");
log("Read Data : ");
console.log(JSON.parse(data));
log("=====================================================================");
fs.appendFile('logleak.json',JSON.stringify(info), function (err)
{
if (err) throw err;
log('The "data to append" was appended to file!');
});



});

}
else
{
console.log("File Not Exists");
}


});

});


My problem is that i need to keep pushing data as an arrray in the JSON object in realtime. And i am not able to achieve this. I need to ensure if the file does not exist it will get created and when there are hits to the node server there would be logs generated which would be added to the JSON file as a JSON structured . But that is not happening. its adding everything but not in a JSOn structure. Please help me how could i achieve this as thsi critical.


Aucun commentaire:

Enregistrer un commentaire