Here is my code which will get data from the postman and write to a file but while writing it replacing the existing data how to fix that in this code.
app.post('/postUser', function (req, res) {
req.on('data', function (data) {
console.log(data.toString());
fs.writeFile('test.json', data, function (err) {
if (!err) {
console.log("Finished writing")
}
});
});
});
My output should be like this.
[
{
"name":"Alpha",
"password":"123",
"id": 1
},
{
"name":"beta",
"password":"123",
"id": 2
}
]
When I use appendFile I am getting output like this :
[
{
"name": "alpha",
"password": "123",
"id": 4
}
] {
"name": "sdad",
"password": "123",
"id": 4
}