I am currently experimenting with writing data to a JSON file using Express.js. Specifically, I am looking to add a new JSON object to the file for each request made. As a beginner in this area, I am feeling quite overwhelmed and uncertain about what steps to take next. Below is the POST request code that I have put together so far. It may seem like a bit of a chaotic mess as I've gathered information from various sources to piece it together. Any guidance or assistance you can offer would be greatly appreciated!
app.post('*/', function(req, res) {
res={
first_name: req.body.first_name,
last_name: req.body.last_name,
reponse1: req.body.reponse1,
reponse2: req.body.reponse2,
};
JSON.stringify(res);
var body = {
table: []
};
body.table.push(res);
filePath = __dirname + '/data.json';
req.on('data', function(data) {
body += data;
});
req.on('end', function (){
fs.appendFile(filePath, body, function() {
res.end();
});
});
});