Pages

Backend for CRUD app using NodeJS, ExpressJS, and MongoDB- Part 1

 In this blog, you will learn to create Project Management System backend with NodeJS, and ExpressJS, which is connected to MongoDB as a database project storage.


 Follow the given steps to build a strong backend of the CRUD app in NodeJS, ExpressJS, and MongoDB.  

We have used four key parts for building the backend architecture: 

  • Routes: Express routers helps create different URL of routes for different requests.

  • Controller: This helps in return response as per the route access.

  • Model: It is a data template that Controller uses in order to interact with MongoDB. 

  • MongoDB: A database that stores real-time data. 


This blog will help you to create a full-stack CRUD app in simple and easy steps.

Prerequisites

Install Visual Studio Code

Install Postman (free version)

Step 1: Stepup NodeJS

First and foremost, install NodeJS on your computer



Create a new folder with the name prj-mng-backned in the directory you prefer. Now start the Visual Studio Code in the folder you just created. 



In your VS Code, create a new terminal.



You can check whether NodeJS is installed properly by running it in the terminal.

node -v


You will find NodeJS's current version that you’ve installed.



Now, run the below command in the terminal.

npm init


The above command is a package.json which has the information of this project like project name, license, author, etc.


Now, we need to set up a development environment by running the given below command.

npm install -D nodemon


After running this command, you would see the package-lock.json file and node_modules folder are created in the project folder. This node_modules folder includes functionality libraries as modules that you can import to the project. On the other hand, the package-lock.json include all recorded dependencies info that you have installed to the project. 


Create a new file name index.js.



Now, pass this in code.

console.log(‘Hello from the other side’);


Then, in the terminal, run the below command.

node index.js


You will see that the words are printed in the terminal.



Step 2: Setup MongoDB Database in the Cloud

Signup for the account for MongoDB. Next, log in and set up your MongoDB. This will help you to connect MongoDB to ExpressJS


Choose JavaScript as per your preferred language.



Now, choose the Free Shared Clustered option.



After that, select the Cloud Provider and Region. Now, press the Create Cluster. It generally takes 2-3 minutes for the new Cluster to create. 



After creating a Cluster, press the Collection button in the cluster. 



Now, tap on the option ‘Add My Own Data’ and fill the name of the database and collection name in the following figure.



Look at the left menu of the page after that and select Database Access to add a new user (using your own username and password) by clicking.



To add a new access connection, return to the Cluster menu and select Connect. When prompted, select our own IP address so that only our location may access the database.



The connection string may then be copied and pasted into a text file to be used in our NodeJS script to connect to this database by clicking on "Choose a connection method" and then copying and pasting the connection string there.



Step 3: Setup MongoDB in NodeJS app

Install MongoDB to the project. Run the below command in the terminal.

npm install mongoose


Inside the index.js file, import the Mongoose module.


The connection string that we earlier stored into a text file should then be assigned to the variable dbURI that you just created.


Change "<password>" in the connection string to the password you entered when you created the user; in my case, that was abcd1234. Change myFirstDatabase to proj-mng instead, as that is the name of the database we named when we started our Cluster.


Now you can use this Mongoose module to connect your database.


The asynchronous method mongoose.connect() delivers a promise in order to print the message "Databse is connected" on the terminal while we wait for it to connect, we utilize the .then command. If the connection is lost, the error will be printed.


Now, run your index.js in the terminal.

node index.js


If you have followed all the steps, you’ll see the below message.


In the next blog, we will mention the steps to create a CRUD application using NodeJS MongoDB. 


No comments:

Post a Comment

Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...