Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!