1

I have ButtonClick.js and TakeData.js files. I defined my json data in TakeData.js as below

    var dataObj = {};
var locationsObj = "locations";
dataObj[locationsObj] = {};
dataObj[locationsObj].source = [];
dataObj[locationsObj].target = [];

When I click the button in ButtonClick.js and the code snippet as below

button()
.container(g2)
.text(text2)
.count(1)
.cb(function()
{
    console.log(dataObj[locationsObj].source[0]);
    console.log(dataObj[locationsObj].source[1]);
    console.log(dataObj[locationsObj].target[0]);
    console.log(dataObj[locationsObj].target[1]);

    console.log("SaveFile");

})();

I want to send json data into the nodejs file writing function as the following.

fs.writeFile('sample.txt', [need to insert my JSON], function(err) {
   if(err) return console.log(err);
    console.log('Hello JSON > sample.txt');
});

How can I do that? Is there another effective way?

zoint
  • 108
  • 2
  • 15

2 Answers2

2

Your backend needs to have an HTTP server listening on some port that is accessible to your frontend. Then your frontend needs to make an AJAX request to your backend (most likely a POST request) and send the required data in the request body.

Now, your backend needs to handle the request, get the data and write it to the file or do whatever you want with that.

Things to keep in mind:

  • use body-parser if you're using Express
  • remember about CORS

It's easier if you use a higher level framework on the backend like Express, Hapi, Restify, LoopBack etc. instead of the low level http module in Node.

It's also easier if you use a framework on the frontend like jQuery, Angular, React, Aurelia, Ember etc.

rsp
  • 107,747
  • 29
  • 201
  • 177
0

The first step is to set up a RESTful POST operation (an HTTP POST) on your server. This is the typical service mechanism for what is sometimes called an AJAX call. Once you have the server set up to receive a string via the POST, you can serialize your objects on the client side and deserialize (reconstitute) the objects on the server side.

To serialize, you can use stringify on the client side. A simple web search for "stringify" will show you different ways to use stringify in a browser-independent, backward compatible way.

stringify(obj)

On the server side, node.js has a global JSON object. Use parse to reconstitute an object or objects from the string. Other major languages now have similar parser methods. 1

JSON.parse(strJSON)

To get started, you can test the mechanism with just one simple object. Then you can aggregate the objects in a JSON array or associative array and send them all at once in a single POST.


[1] There are frameworks that encapsulate this process, but it may be of value to you to NOT initially use them so you get a clear picture of the mechanics of a RESTful POST over the HTTP protocol. It is the key thing that a browser does when you submit an HTML form, and RESTful operations are key communications elements in today's IT world.

Douglas Daseeco
  • 3,475
  • 21
  • 27