![]() ![]() ![]() We're going to showcase two methods for adding files to a zip: one file at a time as well as adding the entire contents of a directory (including its sub-folders). Next, let's get generateZipForPath() setup and learn how to populate our zip. Next, in the callback for the route, we call to the function we'll build next- generateZipForPath()-passing the directory on the server that we want to "zip up." In this case, we'll just use the /lib directory at the root of the server as an example. To do it, we create a new route on our Express app (passed to us via the /index.js file at the root of the boilerplate) with app.use(), passing /zip for the URL that we'll call to. Here, we're using the main API index.js file included in the Node.js server boilerplate (nothing more than a wrapper function to organize code-no special conventions here). Here, we just want a simple route that we can use as a "remote control" to trigger the download of our zip archive and return its contents to us on the client. Import generateZipForPath from "./lib/generateZipForPath" Ĭonst zip = await generateZipForPath("lib") api/index.js import graphql from "./graphql/server" Now, in separate tabs/windows in your terminal, let's start up the server and client with (both use the same command from the root of the cloned directory- nodejs-server-boilerplate or nextjs-boilerplate):Īdding an endpoint where we'll retrieve our zip archiveįirst, let's wire up a new Express.js endpoint in the server that we can call from the client to trigger the download of our zip archive: Terminal cd nextjs-boilerplate & npm installĪnd now, let's add the b64-to-blob and file-saver dependencies we'll need on the client: With that set, next, let's clone the Next.js boilerplate for the front-end: Terminal cd nodejs-server-boilerplate & npm installĪfter those are complete, add the jszip dependency that we'll use to generate our zip archive: Next, install the server boilerplate's built-in dependencies: ![]() ![]() Let's clone each of these now and install the dependencies we'll need for both. For this tutorial, we're going to use the CheatCode Node.js Server Boilerplate as well as the CheatCode Next.js Boilerplate. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |