Set up your Node application for CRUD operations using MongoDB
Introduction
In the realm of modern web development, creating scalable and efficient databases is crucial for handling large volumes of data. MongoDB, a NoSQL database, has gained popularity for its flexibility and scalability. In this blog post, we'll guide you through the process of setting up a MongoDB cluster for a Node.js Express project and implementing basic CRUD (Create, Read, Update, Delete) operations.
Prerequisites
- Node Install in the system.
- One code editor installs in System.
- Postman installs to check ROutes.
Create a mongo cluster account
1 create an account and verify your mail address.
- A few questions will be asked and answer them and click finish button.
- select your plan, provider, regions and write your cluster name by default name is Cluster0. select complete details to click Create.
- Enter User name and password by default the name and password are fill up you have also go with generated name and password. Enter your user name and password to click create user button.
- Add entries to your IP Access List remove from default generated Ip address and click Network Access button for sidebar.
- Add IP address button.
- Click allow access from anywhere button and click confirm button.
- Network access from status is completed your IP address are set completed click database to show the cluster from you have created.
Let's Start a CRUD Operation with Node.js, Express, MongoDB.
- First create a folder name
CRUD operation with node.js, express, MongoDB
and open folder in VS Code (Code editor).
2 initialization your node package module your project. And add project dependences. open terminal and write this command and add dependences.
npm i
npm I nodemon express mongoose cors dotenv
- create
app.js
file add write a basic server code.
- Add script in package. Json page.
- Open terminal and run the code "npm run dev" to run your server
- Click the http://localhost:8000 and show the browser print Node Server.
Connection for mongo cluster to our Project
- First create .env file in the project folder.
- Open your cluster and click connect button.
Open dialog box to click Drivers and copy Add your connection string into your application code.
- Open VS code and paste URL in your .env file in this way.
- After that open app.js file and to write for connection code for MongoDB and some other connectivity to help to run easy our application.
This is updated app.js
file code.
- Now to run
npm run code
in terminal that show the your connection is complete work. and show the message connected to MongoDB.
Our MongoDB connection is completed.
now to go to our CRUD operation.
- Create a
model
folder and createmodel.js
file. This file creates our schema. This is our schema code.
- I have link the model.js file Plese check and write the code
- Create a
controller
folder and createtodo-controller.js
file in our controller folder.
To write our first crate Todo operation controller code for this.
- Now to create a
routes
folder and createrouter.js
file in routes folder. Create a our first route.
- import router file to
app.js
file this is ourapp.js
fie updated code.
- now run the code to write a
npm run dev
in terminal and our server is start. to test our routes to use postman.
Testing Routes
- open Postman And select post request and write a http://localhost:8000/todos and select body and raw to write your Todo. I have attached a screen short for your reference.
- Click send button and check your response in response field.
- To successfully a first create operation are completed. This similar way to create our other operation I have attached our all-operation code file to check and update your file.
Todo-controller.js
I have add my file link to go and check all controller code.
router.js
I have add my router file to go and check router file code.
- Now to run the code that can be complete and server is running fine to check our API I have attached for one-by-one screen short for your reference.
Create Todo
Read Todo
Update Todo
Delete Todo
Our Mongo connection and CRUD operation successfully completed.
Thank you for read a Blog.