0

Hello I am fairly new to NodeJS and its modules but I was wondering if I can get some help with this, I am trying to create a progress bar for an application but it only returns 100% when it completes,I don't think i'm using piping right either but any help would be greatly appreciated!

I am also transmitting data through a socket as well if that makes a difference.

const request = require('request');
const io = require('socket.io')(http)
const fs = require('fs');
var progress = require('progress-stream');

var str = progress({
    time: 1000
});

str.on('progress', function (progress) {
    console.log(Math.round(progress.percentage) + '%');
});


io.on('connection', (socket)=>{
socket.on('fileUploader',()=>{
    let formData = {
            preview_file: fs.createReadStream(filePath};
        request.post({
                url: 'http://httpbin.org/post',
                formData: formData
            }, 
                function optionalCallback(err, httpResponse, body) {
                if (err) {
                    return console.error('upload failed:', err);
                }
                console.log('Upload successful!  Server responded with:', body);
        }).pipe(str);
    })
})

1 Answers1

1

Try using axios, they have onUploadProgress method, which is you can use the "progressEvent" callback from it to get percentage.

The documentation from axios is pretty clear about this.

Rijdzuan Sampoerna
  • 380
  • 1
  • 6
  • 18