1

I hope I'm saying this correctly. What I'm trying to do is write to a json file using fs.writeFile.

I can get it to work using the command line but what I want to do is call a function maybe a button click to update the json file.

I figure I would need some type of call to the node server which is local port 8080. I was researching and seen somebody mention using .post but still can't wrap my head around how to write the logic.

$(".button").on("click", function(event) {

    fs.writeFile("./updateme.json", "{test: 1}", function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    });

});
ifelse
  • 299
  • 2
  • 8
  • 19

1 Answers1

0

Using jQuery along with fs? Wow that could be great! Unfortunately that is not as simple as that!

Let me introduce you to server-side VS client-side JavaScript. Well actually there are a lot of resources on the net about that - just google it, or check the answers to this other StackOverflow question. Basically JavaScript can run either on a browser (Chrome, Mozilla...) or as a program (usually a server written in NodeJS), and while the language is (almost) the same, both platforms don't have the same features.

The script that you're showing should run in a browser, because it's using jQuery and interacting with buttons and stuff (aka the DOM). Can you imagine what a mess it would be if that script could interact with the file system? Any page you'll visit will be able to crawl around in your holiday pictures and other personal stuff you keep on your computer. Bad idea! That is why some libraries like fs are not available in the browser.

Similarly, some libraries like jQuery are not available (or simply useless) in the server, because there is no HTML and user interaction, only headless programs running.


So, what can I do to write a JSON file after a user clicks on a button?

You can set up:

  • A NodeJS server that will write a JSON file
  • Make jQuery call this server with the data to be written after the user clicks on a button

If you want further guidelines on this, tell me in the comments! I'll be ready to edit my question so as to include instructions on setting up such an environment.

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • I totally understand the concept of client vs server side but thanks for the recap. I may have asked my question the wrong way. I'm currently using Vue.js which node is running on my localhost. To run my app I'm using npm start – ifelse Mar 20 '19 at 19:00
  • This looks like what I need https://stackoverflow.com/questions/17981677/using-post-data-to-write-to-local-file-with-node-js-and-express – ifelse Mar 20 '19 at 19:02
  • How can I write an ajax post using fs.writeFile to update a json file? – ifelse Mar 20 '19 at 19:31
  • @ifelse you make your ajax from your browser using `fetch`, then you setup a server that answers to this request, and the servers does the writing – Nino Filiu Mar 20 '19 at 19:52
  • I'm getting an 404 error trying to POST to local file. POST http://0.0.0.0:8080/static/server.js 404 (Not Found). I'm using Vue.js – ifelse Mar 21 '19 at 19:59
  • fetch('/static/server.js', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' }, body: data }); – ifelse Mar 21 '19 at 19:59
  • That's a server error. Your server is running as expected at `0.0.0.0:8080` but is configured to respond with a 404 when the path `/static/server.js` is requested. Without having any server code, it's hard to know how you could handle the issue. – Nino Filiu Mar 21 '19 at 20:36