Mongo DB Setup¶
We will need to setup our Mongo Database before we start working on our REST API.
Mongo DB can be setup on your local machine, but for this tutorial, we will setup it on the Cloud. There are a lot of providers that offer free space for Mongo DB, we will use the official one called MongoDB Atlas (https://www.mongodb.com/atlas/database).
First thing you should do is to visit the link above and click button in "Try Free" in the top-right corner.
Then you will be asked to Register or Login. If you do not have account, please make one and login or if you do, just login.
After you successfully login, you will be asked to accept Privacy Policy. Do that. Then you will be asked some question about the project you will be using the new database for, you can define it like this:
And click "Finish".
After this you will be asked which plan you want to use, for this project we will go with "Free" one. Select it and click "Create".
This will prompt you the next screen like:
For our purposes, we can leave everything as it is and click "Create Cluster".
After Cluster is created, you will be asked to setup it. Something like this:
First thing we will need do, we will have to create our database user. Let's make a new test_user
with some password like Test12345
. (You should not share your passwords with anyone for real projects, this is a demo project so we can)
When our user is created, we want to select that we want connect from our local environment and to allow our IP to connect to it. You can do that by selecting "My Local Environment" card and clicking "Add My Current IP Address" button to whitelist your IP.
Then, you should be able to see your IP listed there and we are ready to go. You can click "Finish and Close" to save changes.
After that, a popup will appear that confirms that changes were applied and you will see the button "Go to Databases". Click that button to see the list of all currently available database we can use.
There, you should be able to see "Cluster0" we made in the previous steps. Now we need to create a database inside that cluster. We can do it by clicking "Cluster 0" as shown on the image below.
After that, we will navigate to "Collections" tab of the Cluster. Then since we do not have any databases create yet we can click "Add My Own Data" in order to create our todo database.
After you click that, the database creation modal should appear. You should enter the following data:
Here we defined our database name as todo_list
and created a collection where we will store todo item called todos
.
After we setup our database for todo list, we want to obtain the connection link so we can connect to MongoDB Cloud database from our Node.js application. To do that we will need to go back to cluster list and click "Connect" button on "Cluster0".
Next you should see the popup that asks you how you want to connect to your database. For this purpose we will use "Connect your application" option.
This should give you a new window with the MongoDB link and instructions on how to connect to it.
Awesome, we got our connection link!
But, as you can see, we need to replace <password>
with our password we created for test_user
and myFirstDatabase
with the name of the database we created todo_list
.
So our final MongoDB connection URL will look like this:
mongodb+srv://test_user:Test12345@cluster0.axh68.mongodb.net/todo_list?retryWrites=true&w=majority
Now we need to test the connection from our Node.js application to this Mongo database link.
To manipulate MongoDB, the idea is to use library called mongoose
for Node.js which helps with object modeling of mongo databases. You can check it here https://mongoosejs.com/.
First, let's navigate to our server-app folder inside VS Code Terminal and add mongoose library to our application by executing
npm install mongoose --save
Then, we will update our index.js
file to connect to the database via URL we created before.
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
const port = 3001;
app.use(cors({origin: '*'}));
const mongoDBLink = 'mongodb+srv://test_user:Test12345@cluster0.axh68.mongodb.net/todo_list?retryWrites=true&w=majority';
mongoose.connect(mongoDBLink);
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});