0

How can I read a file with nodejs, even json, from url?

My file is on a server. I tried with ftp download but it is very inefficient. Now is working on this, but is very slow for download and running...

I need for this type to get data and put in table.

let project_db;
fs.readFile('public/db/db.json', 'utf8', function (err, data) {
    if (err) throw err;
    project_db = JSON.parse(data);
    console.log(project_db.projects.length);
    for(let i = 0; i < project_db.projects.length; i++) {
        $('#projectTable tbody').append(`
            <tr class="rowT">
                <th scope="row" class="scope border-bottom-0">${project_db.projects[i].name}</th>
                <td class="border-bottom-0">${project_db.projects[i].date}</td>
                <td class="border-bottom-0">${(project_db.projects[i].project_constructii == true) ? "DA" : "NU"}</td>
                <td class="border-bottom-0">${(project_db.projects[i].project_hvac == true) ? "DA" : "NU"}</td>
                <td class="border-bottom-0">${(project_db.projects[i].project_instalatii_sanitare == true) ? "DA" : "NU"}</td>
                <td class="border-bottom-0">${(project_db.projects[i].project_instalatii_electrice == true) ? "DA" : "NU"}</td>
                <td class="border-bottom-0">${(project_db.projects[i].project_instalatii_incendiu == true) ? "DA" : "NU"}</td>
                <td class="border-bottom-0"><a href="#" class="btn btn-success">Vezi oferte</a></td>
            </tr>
        `);
    }
});
4
  • 1
    Your code looks somehow as if you try to mix client side (browser) and server side (nodejs) code. Commented Jul 19, 2024 at 11:18
  • 1
    you can't access file system like that in browser. Make a http get request using fetch api and let your server respond to this request with that json file. If its very large file use compression or spit the data into smaller pieces or embed that json data as javascript object in client side code. Commented Jul 19, 2024 at 11:54
  • i am using electron app, it's a browser app inside electron Commented Jul 19, 2024 at 13:11
  • You should have an error in the console(s) telling you what's wrong. Commented Jul 20, 2024 at 10:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.