So, I have a NodeJS API using express and I want to get data from a HTTP request and store it in a local JSON file. This is the file that contains the function to write the local file:
const fs = require('fs').promises;
async function updateData(req,res){
let { name, phone, email, addres, whatsapp, officeHours} = req.body;
let configs={
name:name,
phone:phone,
email:email,
addres:addres,
whatsapp:whatsapp,
officeHours: officeHours
};
let data = JSON.stringify(configs);
console.log(data);
try{
await fs.writeFile('./configs.json', data);
}catch(err){
throw err;
}
}
module.exports = {updateData};
And this is the part of the code I use to call the function with the Post route
routes.post('/storedata', storeData.updateData);
When I send the HTTP request, using Insomnia, I don't get any errors and the data is logged in the console, but the file isn't written.
Try an absolute path like writeFile( __dirname + '/configs.json', data)