Creating a backend for CRUD application with NodeJS, ExpressJS, and MongoDB (Part 1 of 2)
Today, we’ll make a backend for a Project Management System using NodeJS, ExpressJS and MongoDB as our database storage system.
Our backend architecture has four main parts:
- ROUTES: Express routers create different routes (URL) for different requests.
- CONTROLLER: Return response according to which route has been accessed to.
- MODEL: A data template that CONTROLLER uses to interact with the MongoDB.
- MongoDB: Our database that stores actual data.
What we will make together:
PART 1
- Setup Node development environment.
- Setup MongoDB in the cloud.
- Setup MongoDB in NodeJS application
PART 2
- Create Model that communicates with MongoDB.
- Setup the Controller and Express Router for GET request.
- Create the rest of the routes.
Requirement:
Without any further ado, Let’s code!
PART 1
- Setup Node Development Environment.
First, install the NodeJS to our computer.
The installing link is here
Create a folder and name it prj-mng-backend at the preferred directory. Then, open the Visual Studio Code(VS Code) in that newly created folder.
Create a new terminal in your VS Code:
We can check if the NodeJS has been installed properly by running this inside the terminal:
node -v
It shows us the current version of the NodeJS we installed.
Then, continue to run this in the terminal:
npm init
This command a package.json that stored the overall information of this project such as project name, author, license,…
Now, we install the development environment by running this command:
npm install -D nodemon
After the command finishes running, we see the node_modules folder and package-lock.json file are created in our project folder. The node_modules folder contains functionality libraries as modules that we can import to our project to use. The package-lock.json contains all the recorded dependencies information we install to the project.
Create a file name index.js:
And pass in this code:
console.log(‘Hello from the other side’);
Then, in our terminal, run the command
node index.js
We see that the words are printed inside the terminal:
2. Setup MongoDB in the Cloud
First, we need to sign up an account for MongoDB here. Then, log in and setup your MongoDB.
Choose your Javascript as your preferred language.
Then select the FREE Shared Clustered option:
Choose your Cloud Provider and Region. Then click on Create Cluster. It usually take 2–3 minutes for your new Cluster to be created.
After our cluster is created, click on Collection button inside the cluster:
Then click on “Add My Own Data” button and fill out the database name and collection name as in the following figure:
After that, look at the left menu of the page and click on Database Access to add new user (choose your own username and password):
Go back to the Cluster menu, and click on Connect button to add a new access connection. Choose our own IP address when it is prompted, that way, it would allow only our location to access the database.
Then click on “Choose a connection method”, and copy the connection string and paste it into a text file to be used inside our NodeJS script to connect to this database.
3) Setup MongoDB in NodeJS application:
First, we need to install MongoDB to our project. Run this command in your terminal:
npm install mongoose
Inside our index.js file, import the Mongoose module:
Then, create a variable dbURI
and assign it with the connection string that we saved into a text file earlier on.
In the connection string, change <password>
to the one you used when creating the user, in my case, it is abcd1234
. Also, change myFirstDatabase
to proj-mng
which was the database name that we created in our Cluster. So it becomes:
Then we can use the mongoose module to connect to our database:
mongoose.connect()
is an asynchronous method and it returns a promise. Therefore, we use .then
to wait for it to be connected and print out the message Databse is connected
in the terminal. If the connection fails, it will print out the error
instead.
Your index.js file should look similar to this after all the steps above:
Then run your index.js in your terminal:
node index.js
If it is successfully connected, you’ll see the message is displayed:
Congratulation, we just finished Part 1!
Continue to Part 2 here.