I have a json array in a .json file. This file is linked with a node.js file on the server side. This file is handling user comments with a post request. For now, I push a line in the object "comments".
This is my Json file:
[{
"id": 0,
"test_id": "Password test",
"pass": 1,
"fail": 5,
"time": 0.03,
"pass_fail": 20,
"comments":[ {
"comment": "",
"commentuser": ""
}]
},
{
"id": 1,
"test_id": "Password test",
"pass": 1,
"fail": 5,
"time": 0.03,
"pass_fail": 20,
"comments": [{
"comment": "",
"commentuser": ""
}]
},
{
"id": 2,
"test_id": "Form testing",
"pass": 10,
"fail": 0,
"time": 0.09,
"pass_fail": 100,
"comments": [{
"comment": "",
"commentuser": ""
}]
},
{
"id": 3,
"test_id": "Login Test 2",
"pass": 15,
"fail": 0,
"time": 0.04,
"pass_fail": 100,
"comments":[ {
"comment": "",
"commentuser": ""
}]
},
{
"id": 4,
"test_id": "Pen Test",
"pass": 119,
"fail": 2,
"time": 0.04,
"pass_fail": 0,
"comments": [{
"comment": "",
"commentuser": ""
}]
},
{
"id": 5,
"test_id": "Pen Test 2",
"pass": 19,
"fail": 22,
"time": 0.08,
"pass_fail": 1,
"comments":[ {
"comment": "",
"commentuser": ""
}]
},
{
"id": 6,
"test_id": "Pen Test 3",
"pass": 119,
"fail": 2,
"time": 0.04,
"pass_fail": 40,
"comments":[ {
"comment": "",
"commentuser": ""
} ]
},
{
"id": 7,
"test_id": "Login Test 4",
"pass": 119,
"fail": 2,
"time": 0.04,
"pass_fail": 0,
"comments":[ {
"comment": "",
"commentuser": ""
} ]
},
{
"id": 8,
"test_id": "Pen Test 4",
"pass": 119,
"fail": 210,
"time": 0.04,
"pass_fail": 80,
"comments":[ {
"comment": "",
"commentuser": ""
}]
},
{
"id": 9,
"test_id": "Pen Test 5",
"pass": 12,
"fail": 2,
"time": 0.04,
"pass_fail": 0,
"comments":[ {
"comment": "",
"commentuser": ""
} ]
}
]
When a comment is posted the object comments is pushed.
And this is my post request with the push:
app.post('/test/:id/comment', urlencodedParser, function (req, res) {
console.log("User Name : " + req.body.name);
console.log("User Comment: " + req.body.comment);
if (req.body.name && req.body.comment) {
console.log('Your comment was posted !');
res.sendFile(__dirname + '/HTML/comment-success.html');
testing[req.params.id].comments.push({
comment: req.body.comment,
commentuser: req.body.name
})
I want to have the post request such that JS write a new line in the Json file.
Such that the comment is still there once i turn off the server and then on again.
Something like document.write.
How can I implement that ?
Thanks in advance!
No comments:
Post a Comment