1.

How to upload a file using multer in Express.js?

Answer»

We can use Multer to upload files in EXPRESS.js applications. It is an easy-to-use package which adds the capabilities to upload single or multiple files through Express.js. 

First, we shall create a new directory and change it. After that initialize a Node.js app by the NPM init -y command. We are also adding the DEPENDENCIES of express and multer in it.

Next, we will create a server.js file and add the below CODE in it. Here, we are first importing express and multer. After that we are GIVEN the destination directory to store the uploaded file. 

We have created a POST route to upload a single file using multer, using the upload.single(). 

Next, we will start the node server by giving node server.js in the command line. After that in Postman, we will go to the /single route and upload a single file and send it. Give the options, as in the screenshot below. 

Now, we can SEE a new uploads folder been created and the file been uploaded in it.  

Now, we will create a new endpoint to upload multiple files through upload.array(). Here, we are also giving the max number of files that can be uploaded.  

Now, we will send multiple files from Postman to the /bulk route and it will be successful.  

The new files are also saved in the uploads folder.  



Discussion

No Comment Found