• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

122
Views
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 3 years ago · Juan Pablo Isaza
1 answers
Answer question

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 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error