In this tutorial, we are learning Image CRUD Operations In Nodejs, Express, And MongoDB. This Image saves on your computer disk and also if you delete any image, this image will be deleted on your computer disk. This tutorial may help you to get started on how Image handling.
Prerequisites
Before we move on, you’ll need to have the following:
- Node.js installed on your machine.
- setup mongoDB on you computer.
Step-1
Create a Database on your MongoDB and also collection name.
Step-2
open your cmd terminal and type npm init -y
and install following package.
- 1. express
- 2. multer
- 3. mongoose
- 4. body-parser
- 5. nodemon go to https://www.npmjs.com/ sit and find out this package.
Step-3
following file structure do I have
- 1. index.js
- 2. app.js
- 3. src folder
- controller-> UserController.js
- models-> User.js
- routes-> api.js
- utils->fileUpload.js
- 4. storage/images
Step-4
go to the src/routes/api.js file and write as the following image
Step-5
now open app.js and the following image
step-6
then open index.js and do the same thing
Step-7
Now Testing your app. go to Terminal and type nodemon index.js. if all is ok then showing as the image.
Step-8
go to the src/utils/fileUpload.js file and write as the following image
Step-9
Then we create our first router. go to the src/routes/api.js and follow the image.
Step-10
Then write your model on your database.[src/model/User.js]
Step-11
Go to user controller. [src/controllers/UserControllers.js]
Step-12
Now, test api. open your postman and follow the image.
When you click send button then show this information
This image also save on your local computer disk
and save also on your mongoDB database with folder location
Now Image Create is Done.
Then we working Showing this Information.
Step-13
go to api.js and follow the image [src/routes/api.js]
Step-14
go to User Controller [src/controller/UserController.js]
if you click this image which is red mark this image showing on your browser tab.
Now Image Showing is Done.
Then we working Update Information.
Step-15
go to api.js and follow the image [src/routes/api.js]
Step-16
go to User Controller [src/controller/UserController.js]
Go to postman
As a result, previous image delete on your computer disk and save a new image on your computer and also update your mongoDB database.
Now Image Update is Done.
Then we working Delete Image
Step-17
go to api.js and follow the image [src/routes/api.js]
Step-18
go to User Controller [src/controller/UserController.js]
Go to postman
As a result, Image is deleted on local disk and also your database.
your mongoDB Database
here is the GitHub link. I hope you enjoy this article. please give me a star on my GitHub repository. thanks.
Top comments (0)