'Returning updated config.json file (MERN app)
I am using node file system module for the first time to store a config.json file that could be edited from front-end. I am able to read create config.json file and read the file separately.
However, I want to update the file and return the saved file in the same request. How do I do the same efficiently
//To Set config file & return data
router.post('/write-config', adminAuth, (req, res) => {
console.log(`admin wants to update config`) ;
const {config} = req.body ;
try {
fs.writeFile(__dirname+'/data/config.json', JSON.stringify(config), (err) => {
if (err) throw err ;
console.log('Configuration saved successfully.') ;
});
//res.json("ok") ;
/* return the data from file which we just saved */
}
catch(err) {
console.log(err.message) ;
res.status(400).json(err.message) ;
}
}) ;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
