I'm trying to set-up a node-js server that reads a Google Sheets API and returns it as JSON, refreshing itself every two seconds (so that as and when updates are made, the server just sends it correctly to my web page.)
Sheets API is working great.
my gigData array is accessible in my index.js file and loads okay (though the first time it does it's an empty array/null).
I just need the server to refresh / or the browser to refresh... or something.
So that when i do my fetch() method i know it'll get the data okay?
I was thinking a setInterval or something but i cant get it to work.
Where am i going wrong?
Was thinking something like:
setInterval(function(){
app.get('/', function(req, res) {
res.json(getData);
});
}, 2000)
INDEX.JS
var express = require('express');
var app = express();
var getData = require('./getData');
// ^ taking in my data/result of my api above ^
app.set('port', (process.env.PORT || 5000));
app.get('/', function(req, res) {
res.json(getData);
});
// ^ my root express page, serves the json fine. ^
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
setInterval(function() {
}, 2000);
// ^ how do i get this to re-acquire the getData json and re-serve it?
via NewbieAid
No comments:
Post a Comment