Welcome folks today in this blog post we will be downloading multiple files
at once using express-zip
library in browser using node.js
and express
in javascript. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js
project using the below command
npm init -y
npm i express
npm i express-zip
Now we need to make the index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 |
var app = require('express')(); let zip = require('express-zip') app.get('/', function(req, res) { res.zip([ { path: 'file1.pdf', name: 'file1.pdf' }, { path: 'file2.pdf', name: 'file2.pdf' } ]); }); app.listen(3000); |
As you can see we are importing the express
and express-zip
libraries at the top and then we are making the get
route at the /
homepage and inside it we are using the zip()
method inside which we are passing the array
of files in which we have two properties
in which we are passing the path
of the file and then we are passing the filename
of the file to be compressed and then we are starting the node.js
and express
at port 3000.