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

148
Visualizações
Read fs without restarting server

I was using readdirsync() to get all the file names in a directory then send it to the front end and it works fine. The problem is if a new file is created in the directory after readdirsync run it the won't update the files name array to send to the back end. How can I listen if a new file exists and resend the data to the client-side without having to reload the server manually? Is this possible? Any help is appreciated. Thanks in advance.

const fs = require("fs");

var currentData = []
fs.readdirSync('./dirPATH').forEach(file => {
    currentData.push(file);
});

//what i tried but this won't work. still requires manual server restart:
function dataExchange() {
    app.post("/getData", (req, res) => {
        try {
            res.send(currentData);
        } catch (error) {
            console.log(error);
            res.status(400);
        }
    })
}

dataExchange()
setInterval(() => {
    dataExchange()
}, 2000)

about 4 years ago · Juan Pablo Isaza
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