1

I'm pretty new to JS and Node so it might be a dumb question. Ive look everywhere and I can't figure how to get a response from the Server. The goal is to be able to do a progress bar so I need to return the bytes download / Total bytes.

For now i'm able to have the information but only on the server, I can't figure how to send it to the front end.

Heres what I did so far.

Server.js

app.get('/', async (req, res) => {
    const response =
    fetch(`https://api.github.com/repos/${options.gitUser}/${options.gitRepo}/releases/latest`)
    .then(response => response.json()).then(data => { json = data; })
    .then(() => {
        let zip;
        for (i = 0; i < json['assets'].length; i++) {
            if (json['assets'][i]['name'] === `${options.softName}`) zip = json['assets'][i];
        }
        let received_bytes = 0;
        let total_bytes = 0;
        
        var req = request(
            {
                method: 'GET',
                uri: zip['browser_download_url']
            }
        );
        
        var out = fs.createWriteStream(`C:\\Users\\${options.userName}\\AppData\\Roaming\\Camnor\\Camsoft\\${options.softName}`);
        req.pipe(out);
        
        req.on('response', data => {
            total_bytes = parseInt(data.headers['content-length']);
        });

        req.on('data', chunk => {
            received_bytes += chunk.length;
            console.log(received_bytes, total_bytes); <== The data I want in the front end
        });
        
    });
    
})

On the client side I just call

Neutralino.os.execCommand('server-win.exe'); const response = fetch('http://localhost:3000') Ive try a lot of thing but I can't figure how to send the information to the front end. The zip file is downloaded properlly on the correct folder. Everything else work. Thanks

Meilleur102
  • 25
  • 1
  • 8
  • The basic pattern is: `app.get(‘/route/‘, (req, res) => res.send(‘put here message for client’)` You have to use res from `app.get` callback. – Fide Oct 22 '21 at 21:06
  • The thing is the res will be send only when the progress is complete. If you look at my code, the last line is the part of the code I want to have back. – Meilleur102 Oct 23 '21 at 16:39
  • Why don't you send it with `res.send({received_bytes, total_bytes})`? Or you want to send it as progress bar? – Fide Oct 24 '21 at 18:09

0 Answers0