Home > Net >  React and Node app download zip file from REST API
React and Node app download zip file from REST API

Time:11-10

I have a React and Node app and I have a table with download button. When I click the download button it makes a request to the node app which I want it to make a call to a REST API to download a zip file in the browser.

I managed to download the zip file by linking the REST API url directly from React but my manager told me not to expose the API url and mask it through Node.

I tried different methods that I found on the web but none of them work, just one that downloaded a zip that contained a file that contained base64 of the files probably.

I would like a solution both for React and Node how to download from a REST API.

Thanks

React code:

 $("#example tbody").on('click', '#buton123', function() {
   
   
  var 
   id=response.data.mesaje[$(this).closest('tr').index()].id;
 
   article2 = {id2:id} 
   axios.post('http://localhost:3001/descarcare',article2)
   .then((respo 
    nse)=>{
   //How to download zip from node 
   })

   });

Node code:

app.post('/descarcare', function(req, res) {

var id = req.body.id2;
console.log(id);


axios.get('http://restapi.com/aaa?id=123').then((response)=>{
//Code to download zip from rest api
)}




});

CodePudding user response:

You can do one thing make api request to your source zip file containing rest api from nodejs and get stream data and then pipe it res. In this way you can get stream data directly to react js application from nodejs application

CodePudding user response:

Check jszip and jszip-utils libraries available for zipping the content.

  • Related