Build a CRUD Operations with Node JS, Express & MongoDB

CRUD stands for Create, Read, Update and Delete. It is a set of operations we get servers to execute (POST, GET, PUT and DELETE respectively). Create means inserting data into the database using INSERT SQL statement. Read means reading data from the database using SELECT SQL statement. Update means updating records using the UPDATE SQL query. Finally, Delete means deleting data from database using DELETE SQL statements.
In this tutorial, We will share how to Build CRUD Operations with Node JS, Express, and MongoDB. This is a very simple example, you can just copy paste and change according to your requirement.
Before started to implement the Newsletter Email Subscription with PHP and MySQL, look files structure:
  • crud-operation-in-nodejs-with-mongodb
    • models
      • DBconfig.js
      • emp.model.js
    • controllers
      • empController.js
    • package.json
    • server.js
    • node_modules
    • views
      • css
        • style.css
      • images
    • assets
      • employee
        • addupdate.hbs
        • list.hbs
      • templates
        • mainLayout.hbs
Step 1: Create Project
First, we will create a application directory crud-operation-in-nodejs-with-mongodb.
Go to ROOT directory of application and type npm init command to initialize our application with a package.json file.
The package.json file will be created at the ROOT of application directory.
Change main file from index.js file to server.js that we will create later as it will be entry point of application.
Step 2: Install Project Dependencies
Dependencies will be added in the package.json file.
There will also be node_modules folder added at the root of our application with Node modules.
Create DB config file
Create a config file named DBconfig.js inside “models” folder.
Create a model file
Create a model file named emp.model.js inside “models” folder.
Create a controller file
Create a controller file named empController.js inside “controllers” folder.
Create a view file
Create a view file named addupdate.hbs inside “views/employee” folder.
Create a view file named list.hbs inside “views/employee” folder.
Create files named (mainLayout.hbs)
This file contains the mainLayout section of the webpage. The Bootstrap library is used to provide a better UI.
Step 3: Setup Application Server.js file
We will create server.js file at the ROOT of application. We will add following code to our server.js file.
Note: At the top of file, we will import the required modules.
Run Server following command:
Nodemon restarts the server automatically whenever you save a file that the server uses:
CRUD Operations with Node JS, Express, and MongoDB – Output
List Employee screen
Create Employee screen