1

How do i fetch values at regular intervals in Express.js from an external json file?

Gyanesh Gouraw
  • 1,991
  • 4
  • 23
  • 31

1 Answers1

1

Well this is very vague, but what you can do is setup a function that executes at regular intervals and fetches values from a JSON file.

// Fetch values every 3 seconeds
setInterval(function(){

  var fs = require('fs');
  var file = __dirname + '/test.json';

  fs.readFile(file, 'utf8', function (err, data) {
    if (err) {
      console.log('Error: ' + err);
      return;
    }

    data = JSON.parse(data);

    // Now you have a JSON object from which you can extract values.
    // You can save those values somewhere or just log them to the console
    console.dir(data);
  });

}, 3000);
turnt
  • 3,235
  • 5
  • 23
  • 39
  • By 'external', I believe OP meant 'remote' json file. If that is the case, replace function in setInterval with similar code in this post: http://stackoverflow.com/questions/9577611/http-get-request-in-node-js-express – palanik Mar 04 '14 at 04:51