Creating a zip file dynamically on requested by the client in NodeJS/Loopback

I had been working on a requirement to provide a sample CSV file for the import of memberships for organizations. The challenge was to make the users aware of how each field needs to be defined since some of the fields were required, some were optional, some supported only a certain set of strings.

We added these instructions to the start of the CSV but then users were confused about what rows to keep in the CSV before the import.

We then thought of providing the sample CSV file and the instructions as two different files respectively zipped together when the user downloads. But since our sample CSV file was autogenerated based on the passed parameters we couldn't keep our files n the filesystem zipped and then sent. The option left with us was to stream the zip file that we would build on the fly. That's when we came across the “archiver” npm package.

There have been various examples of how to use the “archiver” node module with express. but something I could hardly find was the one with loopback (loopback.io). Most of the examples out there didn't work for me and always returned a “Failed — No file” error on triggering a download. Hence after a lot of trials and errors, something worked.

The below code snippet is pretty much straight forward on how to achieve archiving at a loopback based server and download from the browsers.

I hope the above code helps someone who is struggling to create a dynamic archive file in nodejs/express framework.

If the above content helped you please leave a 👏. It would encourage me to write more such articles.

--

--

--

Tech Enthusiast | Programmer | Architect | Likes to Travel | Interests in Technology, History, Languages, Science

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Robinhood Clone with React Native | Chapter 1: The Navigation

Introduction To Routing In Angular

刷題日記(2)Merge Two Sorted Lists

Create your own form validation

Using a React form to send information directly to your email

Callbacks and Promises — When Javascript Plays Hard to Get

flowchart of what happens after promise in JS

Enhance your web apps with a vibration!!

Angular Workspace Configuration

Angular

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Basavaraj V

Basavaraj V

Tech Enthusiast | Programmer | Architect | Likes to Travel | Interests in Technology, History, Languages, Science

More from Medium

Instagram — Add a Feature

AVL Tree vs RB Tree with Applications

API — definition, mechanism, status code, security