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

235
Visualizações
Variabel won't keep values using Heroku

A very small example

let value = null;
const getValues = () => {
  fetch('/third-party-api')
    .then(res => res.json())
    .then(data => {
      value = data;
    })
}

getValues();

app.get("/values", async (req, res) => {
  res.json(value);
});

When it is getting deployed, it will work. But after x-amount of time (sometimes hours, sometimes it will work the whole day), the value will get back to being null. I am paying for Heroku, so the server will never sleep, and this is why it doesn't make sense to me why this happens.

Can I somehow prevent this, so the value won't get back to being null?

Why I'm doing this:

The third-party API is huge and takes around 30 sec to fetch. So I simply create this tiny API to add pagination and only call the third party on build

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

0

You can simply use middleware in your route if you use this method specifically in this route. You can do something like this:

let value = null;
const getValues = async (req, res, next) => {
  if (!value) { 
    next();
  }

  try {
  const res = await fetch('/third-party-api');
  const resJson = JSON.parse(res);
  value = resJson;
  next();
  } catch (e) {
     res.send({error: e});
  }
}

// getValues(); remove this call

app.get("/values", getValues ,async (req, res) => {
  res.json(value);
});
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