I am making file download in client side using nodejs from box.

I have one box url.

When user hit box url in browser,

https://www.example.com?url=https://box.com/file/123

The process i am following is:

In node js fetching the box url and making a request to box.com

  const obj = urlParse.parse(fileUrl, true);
  const creq = https.request(obj, (cres) => {

  cres.setEncoding('utf8');

if (cres.statusCode === 200) {
  res.writeHead(cres.statusCode, 
    { 
      'Content-Type': 'application/octet-stream',
      'Content-Disposition' : cres.headers['content-disposition']
    }
  );
} else {
  res.writeHead(cres.statusCode, 
    { 
      'Content-Type': 'application/json'
    });
}

// wait for data
cres.on('data', function(chunk){
  res.write(chunk);
});

cres.on('close', function(){
  return res.end();
});

cres.on('end', function(){

  return res.end();
});

}).on('error', function(e) {
return res.end(e.message);
});

creq.end();

The issue i am facing is, In production if some times network is not good or speed in production - node server, then in browser it showing error as Failed network - Server error

So I would like to implement, pause when network is not good and resume if network is good.

Can any one please help me to handle this.

Process is:

request: Browser -> Node js -> Box.com Response: Box.com -> Node js -> Browser (fetch file data in chunks by chunks)

If network is not good, getting error as failed network.

So, if network is not good, I need to pause file download in node js and browser and when network is good, it will automatically resume download in browser and node js.

So, The file will get downloads even in bad network instead of showing Failed network - Server error info to user.

0 Answers