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

166
Visualizações
One json file is written and read by node.js and some other application problem

I have a node.js server (express.js) and I have a json file that I use to read and write data (simple api). When the node.js server is stopped, that other application adds data to the json file without any problems. When the node.js server is started that other application sees that json file as an empty file, probably because the node.js server keeps it open. In node.js I use fs.readFile and it closes the json file after reading, so I don't think that's the problem. All this is happening on a local windows machine. Has anyone had a similar problem sharing a single file with node.js and some other application? If anyone had a direction to guide me, where to look for a solution.

Thank you for your time!

@jfriend00 As for that other application, it was written in clarion and I don't have access at the moment. That application rarely writes data to that file. Here's how I read and write the file:

function load(fileName, path, callback) {    
    let jsonPath = "";
    if(fileName == "companies") {
        jsonPath = __dirname + "\\..\\..\\";
    } else {
        jsonPath = path;
    }    
    fs.readFile(jsonPath + fileName.toUpperCase() + '.JSON', function (err, data) {    
        if (err) {
            if (err.code === "ENOENT") {                
                console.log("File '" + fileName.toUpperCase() + ".JSON' not found!");
                callback([]);
                return;
            } else {
                throw err;
            }
        }
        callback(JSON.parse(data.toString()));
    });
} 


function save(fileName, path, array) {
    let jsonPath = "";
    if(fileName == "companies") {
        jsonPath = __dirname + "\\..\\..\\";
    } else {
        jsonPath = path;
    } 
    fs.writeFile(jsonPath + fileName.toUpperCase() + '.JSON', JSON.stringify(array), function (err) {
        if (err) {
            throw err;
        }
    });
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That software which I share json with, needs nice format, not an one line data. So, I have to added two more parameters in JSON.stringify(array, null, 2) to format data nicely wen a calling fs.writeFile method. I totally forgot this. This was two days of agony.

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