1

Is there a way to save data from an API to a JSON file, with NodeJS using XMLHttpRequest?

The API data is supposed to be displayed on a website, but the API is increcibly slow, so to combat this I would save the data on the server and display the newest data on the website every 5 minutes.

The API is public, the link is http://lonobox.com/api/index.php?id=100002519 if that helps.

Any help is greatly appreciated.

2
  • 1
    It should be possible.. What have you tried so far? Commented Dec 14, 2017 at 20:18
  • Read up on how to set up a cron job Commented Dec 14, 2017 at 20:25

1 Answer 1

1

Hey I do a similar thing with a node server that performs basic function on JSON data that I use at work. When it comes to saving the data I just POST it to the server.

But when it come to reading the data I use a XMLHttpRequest to do it, let me illustrate how it works which should give you a good start.

POST file to server.

function processFile(e) {
    var file = e.target.result,results;


    if (file && file.length) {
        $.ajax({
          type: "POST",
          url: "http://localhost:8080/",
          data: {
            'data': file
        }
        }).done(function(msg) {
            appendText("Data Saved: " + msg);
        });
    }
}

From here you can fetch the data with XMLHttpRequest like so...

function getFile(){
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", "filename.json", false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
             var fileText = rawFile.responseText;
         }
     }
 }
 rawFile.send(null);
}

Server Code

app.post('/', function(req, res) {
    var fileLoc = __dirname.split("\\").length > 1 ? __dirname + "\\public\\filename.json" : __dirname + "/public/filename.json";
    fs.writeFile(fileLoc, req.body.data, function(err) {
        if (err) {
            res.send('Something when wrong: ' + err);
        } else {
            res.send('Saved!');
        }
    })
  });

Server side requires FS and I use Express for routing.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I will try this. My goal is also to send a new request if the JSON file is changed, but without making more than 1 request to the API. My current "matching data function" is not very good, it will keep making new requests till it gets an answer, it will end up making a lot of requests since it takes 30 seconds+ for the API to respond.
No worries let me know how you get along. Hmmm sounds like you could use a callback somewhere in your code if you're making too many requests. Can you clarify how your code works for me? - Request API for JSON (Takes ages?) - Match against current saved JSON for differences? - If changed save new JSON?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.