I'm trying to write a key-value pair in my json file using fs.appendFile().
Here's my code:
router.post('/add', function(req, res) {
var article = {
title: req.body.title,
content: req.body.content
}
var articleData = {};
articleData[article.title] = article.content;
var textData = JSON.stringify(articleData, null, 2);
fs.appendFile('model/text.json', textData, 'utf8', finished);
function finished () {
console.log('Finished writing');
}
});
But in my text.json file, I'm only getting this:
{
"test1": "test1"
}{
"test2": "test2"
}
I cannot manage to append it like this:
{
"test1": "test1",
"test2": "test2"
}