DEV Community

syed kamruzzaman
syed kamruzzaman

Posted on • Updated on

Nodejs, Express, and MongoDB Image CRUD API and save on Local computer disk

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:

  1. Node.js installed on your machine.
  2. setup mongoDB on you computer.

Step-1
Create a Database on your MongoDB and also collection name.
Image description

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.

Image description

Step-3
following file structure do I have

  • 1. index.js
  • 2. app.js
  • 3. src folder
    1. controller-> UserController.js
    2. models-> User.js
    3. routes-> api.js
    4. utils->fileUpload.js
  • 4. storage/images

Image description

Step-4
go to the src/routes/api.js file and write as the following image

Image description

Step-5
now open app.js and the following image

Image description

step-6
then open index.js and do the same thing

Image description

Step-7
Now Testing your app. go to Terminal and type nodemon index.js. if all is ok then showing as the image.

Image description

Step-8
go to the src/utils/fileUpload.js file and write as the following image

Image description

Step-9
Then we create our first router. go to the src/routes/api.js and follow the image.

Image description

Step-10
Then write your model on your database.[src/model/User.js]

Image description

Step-11
Go to user controller. [src/controllers/UserControllers.js]

Image description

Step-12
Now, test api. open your postman and follow the image.

Image description

When you click send button then show this information

Image description

This image also save on your local computer disk

Image description
and save also on your mongoDB database with folder location

Image description

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]

Image description

Step-14
go to User Controller [src/controller/UserController.js]

Image description
Go to postman

Image description
if you click this image which is red mark this image showing on your browser tab.

Image description

Now Image Showing is Done.
Then we working Update Information.

Step-15
go to api.js and follow the image [src/routes/api.js]

Image description

Step-16
go to User Controller [src/controller/UserController.js]

Image description

Go to postman

Image description

As a result, previous image delete on your computer disk and save a new image on your computer and also update your mongoDB database.

Image description

Now Image Update is Done.
Then we working Delete Image

Step-17
go to api.js and follow the image [src/routes/api.js]

Image description

Step-18
go to User Controller [src/controller/UserController.js]

Image description

Go to postman

Image description

As a result, Image is deleted on local disk and also your database.

your local disk
Image description

your mongoDB Database

Image description

here is the GitHub link. I hope you enjoy this article. please give me a star on my GitHub repository. thanks.

https://github.com/kamruzzamanripon/express-image-crud-api

Top comments (0)