[Vuejs]-Edit JSON file on local server

0👍

You would need to create a new endpoint to accept a POST request that would accept your new config and trigger a re-write of the file on your server.

app.post('/api/settings', postSettings);

function postSettings(req,res){
  var fileContent = JSON.stringify(req.body.settings);

  fs.writeFile('config.json', content, 'utf8', function (err) {
    if (err) {
      res.status(400).send({error: 'some error'})
    } else {
      res.send({message: 'success'})
    }
  }); 
}

A few notes:

  • My code above assumes the front end will send an object with a field called settings, which will contain the details of the file you want to write.

  • You may know this, but it is recommended to store data in a database, as opposed to allowing the front end to re-write a file on the server

Leave a comment