0

I’m developing a very simple web application, it just display’s static data and has no server, just html, css and javascript. One feature this application needs is to read and write to a log file every time the application is launched (index.html is opened in a browser). The log file is extremely simple, it should log 1 or 0 to a .txt file every time the app is launched. When the app is launched (open index.html) it should read the last value of the log file, save it as a variable for further logic and than write to the log file again. Is this possible with pure javascript/ jQuery? I’m trying to avoid using a node.js server or any databases as my client wants this as simple as possible. I’ve tried this jQuery below but I get this error… “jquery-2.2.3.js:9203 XMLHttpRequest cannot load’”. Any help is greatly appreciated, Thanks.

$(document).ready(function() {
  // Get Log File
  function getLog() {
      $.ajax({
          url: 'file:///xxx/xxx/Desktop/app/logfile.txt', // fake path obvi
          dataType: 'text',
          success: function(text) {
              $("#Adaptors").text(text);
              setTimeout(getLog, 10000); // refresh every 10 seconds
              console.log("got Log: ", text);
          }
      })
  }

  getLog();
});
  • https://developer.mozilla.org/en-US/docs/Web/API/File_System_API, as well as http://stackoverflow.com/questions/3950131/how-can-i-create-a-file-for-storage-on-the-client-side-with-javascript, http://stackoverflow.com/questions/13405129/javascript-create-and-save-file, http://stackoverflow.com/questions/21012580/is-it-possible-to-write-data-to-file-using-only-javascript, and probably many more. Short answer: You can't make an HTTP request to the file system because the file system isn't a web server. – David Jun 14 '16 at 17:44
  • 1
    To follow up why this is difficult, it is because it is a HUGE security concern. If Javascript on any website you visit could immediately read/write files to anywhere on your filesystem, you would have viruses/all your information stolen from every shady website you visited. – Samuel Davidson Jun 14 '16 at 17:46
  • I was nervous about this but it makes perfect sense, thanks for clarifying this for me and saving me time. – NodeDeveloper102 Jun 14 '16 at 17:53

1 Answers1

1

It is not really possible or convenient for client-side javascript to write anywhere on your filesystem. What you should do instead is create an endpoint on your server dedicating to logging.

Your server-side javascript has the ability to read/write from the filesystem and therefor any logging must be done from there. I know firsthand this is easy with a Node / ExpressJS server.

For example your client could POST to a logging endpoint such as /logs/error with whatever logs/errors in a JSON object. The server would then format and write that log to the filesystem.

Edit: I should clarify that it is possible but not before the user tediously gives permission for every interaction with the filesystem the javascript wants to perform. Which not really what you want for logging.

Samuel Davidson
  • 783
  • 6
  • 16