javascriptangularjselectrontaffydb

AngularJS + Electron: Save Data to JSON File


I'm very new to AngularJS and Electron, and i'm currently working on a simple desktop app that reads data from a JSON file and allows the user also update and delete data. I'm also using TaffyDB to query the data. I'm able get data from the JSON file but unable to store it in the JSON file.

What i tried so far was this:

myApp.controller('homeController', ['$scope', '$http', function($scope, $http) {

    $scope.saveData = function() 
    {
        var data = $scope.data;

        $http.post('/src/db/db.json', data).then(function (response) {

            console.log(response);

        }, function (response) {

            console.log(response);

        });
    };

}]);

Executing the event on the browser i get the following error:

POST http://127.0.0.1:64262/src/db/db.json 404 (Not Found)

This is normal because we cannot access the user's filesystem from javascript.

When i execute the app as a Electron package, i get the following:

Object {data: Array[2], status: 200, config: Object, statusText: "OK"}

But the file was not modified.

I would like to know if there is any way i can accomplish this using AngularJS and Electron.

Important Note: This app needs to run as a standalone desktop application. In other words, my client doesn't what to install other software or apps to be able to use this application.


Solution

  • You can't write to the file system using HTTP, that's for the web only.

    Electron is built on Node.js. So take a look at the Node.js File System module. For example:

    fs.writeFile('/src/db/db.json', data, (err) => {
      if (err) throw err;
      console.log('It\'s saved!');
    });