Single and multiple images upload and remove from Cloudinary using Node JS, Multer, MongoDB
Tags: node, mongodb, cloudinary, multer
Published: 2022-02-23 07:30:02

This system is working without using the file system of node. And I'm also using Thunder client (VS Code extension) for sending request, but you can also use Postman.

First create an ordinary Node JS application.

This is my folder structure and package.json file. I will be using these packages to do the work.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/efvqny3j4czi6nbnrx4h.png)

Go to [Cloudinary](https://cloudinary.com) and create a free account. After creating an account, you will find CLOUDINARY_NAME, CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET in your account dashboard.

1# .env file
=> This file will contain all of the secret you don't want to show others
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ytfdmqbp34x7yayaa6j4.png)

2# cloudinary.js file
=> Connects your application to cloudinary. Remember to use **_dotenv_**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7h18dulq28y7bxikolao.png)

3# cloudinaryAccountStorage.js file
=> This file will create a seperate folder to store all of your images in Cloudinary.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69vrbqhodvr1t1wcy4fk.png)

4# people.js file
=> People schema and people model for mongoose to store data in DB.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xiairi3u2h9ztuw9z6uk.png)

5# message.js file
=> Message schema and message model for mongoose to store data in DB.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmtnjsnntga4lwb0dugq.png)

6# server.js file
=> Every file is connected here. Remember to import and use **_dotenv_**.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j00722u6tfleij3fwkwj.png)

7# uploadRouter.js file
=> Main routing file of this application. Send different request to different path.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ko8fealpw4qywtxd995.png)

8# imageUploader.js file
=> Universal image uploader file. Maximum file size, maximum file number, file type, and validation can control using this function.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/na04ivpa1zz810i8679s.png)

9# avatarUpload.js file
=> Sending params to imageUploader.js for file size, file number and file type with error message. Most of the tutorial uses upload.single(), upload.array(), upload.fields() but I'm using upload.any()() to give you some idea how it works
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0fngw2igy0fwdohdega.png)

10# attachmentUpload.js file
=> Sending params to imageUploader.js for file size, file number and file type with error message. Most of the tutorial uses upload.single(), upload.array(), upload.fields() but I'm using upload.any()() to give you some idea how it works
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rbjwb2k5es5jawruoh3.png)

11# uploadController.js file
=> Structure and async function name of controller function. This file control upload and remove images from Cloudinary. It also sends json data to database.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrvec6fa2wdoxngbprrh.png)

11.1# addUser function of uploadController.js
=> Upload single image to Cloudinary and send public_id, secure_url to database of cloudinary upload.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s3fdtgtt7anlczsvr5qd.png)
Result of addUser function
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dc94fzwhltpgoj2fpul.png)

11.2# deleteUser function of uploadController.js
=> Remove single image from Cloudinary also remove from database.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/290kcf3q8mwjwy6hwglf.png)
Result of deleteUser function
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3qbbs19fsf42n0ur2sz3.png)

11.3# sendMessage function of uploadController.js
=> Upload multiple images to Cloudinary and send public_id, secure_url to database as an object of different cloudinary upload.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/30px4n9010btma9duqtu.png)
Result of sendMessage function
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs00abzwsr3msx61v1xw.png)

11.4# removeMsgAndAttachments function of uploadController.js
=> Remove multiple images from Cloudinary also remove from database.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c008j5xb2tvsy1jemum8.png)
Result of removeMsgAndAttachments function
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mmruuw1gdk06ew48bdjz.png)

[Git repo](https://github.com/arifulhasantotul/node_mongodb_cloudinary_multer_uploadanddelete) of this code.
Thank you all.