Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
set an interval to refresh an api after x time

i have a node server that fetchs api from NASA apoc api i want the node server to make a request evrey 24 hours and get the latest JSON ive tried to set an interval via function and it didn't work

import http from 'http';
import fetch from 'node-fetch';
import "./schema.mjs"
import { lastSol } from './schema.mjs';
const hostname = 'localhost';
const port = 3001;

const server = http.createServer(async (req, res) => {
  
  
  res.statusCode = 200;
  res.setHeader('Access-Control-Allow-Origin', '*'); // allow all domains to access the data
  res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET'); // allow get domains to access the data
  res.setHeader('Access-Control-Max-Age', 2592000);  
  res.setHeader('Content-Type', 'application/json');  // set the content type to json

  let apiURL = 'https://api.nasa.gov/planetary/apod?api_key=XXXX';
  
  const response = await fetch(apiURL); // fetching the data from the api
  const apod = await response.json();
  function callApiEveryCSeconds(n) {
    setInterval(apod, n * 1000);
}

callApiEveryCSeconds(1);


  res.end(JSON.stringify(callApiEveryCSeconds)); // return JSON response

});

console.log(lastSol())

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You would be better off running the API and creating a separate worker task that fetches data from the API and stores it on a time interval.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda