

The encoding being set to null tells request() that this is binary data. If this were a production application, you'd probably be pulling from a blob storage or a local filesystem, but this is a good example because it shows how we're using request to download a file, and then passing those results immediately to the archive. In this example, we're actually pulling the files from externally hosted systems, so we're downloading them first, and then adding them to an archive. The materials object is an array of objects that each have folder and url properties.

Also, as I'm sure you've noticed by now, I'm using TypeScript, but other than the type declarations, all this code will work in JavaScript as well.

app.get("/example", async (req, res) => ) Ī couple of quick notes: I've trimmed some code out of this. Let's go over the route first, and then come back to the utility function. I'm also putting that import statement in the main index.ts file that I use as the entry point for the Express application and the routing. …and I abstracted the zipping code into a util.ts file, importing the package at the top: import * as archiver from "archiver" I went with the archiver package: npm install archiver -save Since the LMS is a web application, we want a download link that streams the contents back to the student on the fly. The nature of the downloaded materials, and the fact that students need to download multiple items, means that we want to zip those files up. Some of these materials are PowerPoint documents, some of Word documents, and some are even videos. In one particular use case, students can download all the materials for a particular course, for a particular week, for a particular day, or for an individual activity. As I've been working on the front-end of this learning management system (LMS) that my team and I have been building at work, I've had to build out a handful of functionality to deal with the ways that students interact with the courses-whether course content, course meta data, or course materials.
