Database Connectivity with MongoDB using Nodemon

How to create a database connection with MongoDB While working on MERN NodeJS, ReactJS and express application.

Database Connectivity and  Nodemon.

In this tutorial, we will connect to the MongoDB server locally and will learn about its different parameters. We can also connect the database serving from the cloud using MongoDB atlas. 

We will also see the use of the Nodemon library as the restarting server manually after every change is a bit of a boring process. Nodemon package helps us to restart the server as soon as we save any changes in our files.

What is MongoDB?

⇒ As in the installation tutorial we see how to set up MongoDB in our machine locally as well as on clouds using MongoDB atlas.

⇒ Now we will see how to use the database. Despite the local or cloud database, it is connected in a similar way using mongoose.

What is mongoose ??

 ⇒ Mongoose is the object data modeling(ODM) library for node and MongoDB. It is used to create a structure of Schema(object) that is going to be stored in a database. It is also used to create a relation between different schema. Eg:- Post schema and comments schema are related.

⇒ We Use a function of mongoose called connect() to establish the connection to the database.

⇒ We pass three parameters along with mongoose. connect functions. These parameters are passed to avoid depreciation errors. As new code is added to the database of node and it’s developing vigorously.

  • newUrlParser: it is used to parse the new URL string that will be coming in the site.
  • useUnifiedTopology: significant refactor of how it handles monitoring all the servers in a replica set or sharded cluster.
  • useCreateIndex: The MongoDB driver deprecated this function in favor of createIndex().

To get Mongoose we use the following command in the library of project.

PS D:\work\Codebun\tutorial> npm i mongoose

It will install the mongoose library on the machine and add the package to the package.json file.

Code to connect the MongoDB database.

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/Tutorial',{
    useNewUrlParser:true,
    useUnifiedTopology:true,
    useCreateIndex:true
}).then(()=>{
    console.log("DB Connected Successfully");
});

Explanation:-

  1. Require the mongoose using mongoose function
  2. Use mongoose.connect function to connect to the local database
  3. The parameters passed are the URL of the database as a string  
    mongodb://localhost:27017/database_name
  4. This is the address of MongoDB on any machine locally and the last one is the database name as needed.
  5. We use then() function to check whether the connection is successful or not. Hence we give a callback of success message.

Creating a server and Add this code to it

const express = require("express");
const app = express();
const mongoose = require('mongoose');
 
mongoose.connect('mongodb://localhost:27017/Tutorial',{
    useNewUrlParser:true,
    useUnifiedTopology:true,
    useCreateIndex:true
}).then(()=>{
    console.log("DB Connected Successfully");
});
 
app.get("/",(req, res)=>{
    res.send("Hello World");
});
 
const PORT = 2000;
app.listen(PORT,()=>{
    console.log(`Listening on port ${PORT}`);
});

Run the app using the command

PS D:\work\Codebun\tutorial> node app.js

The o/p in the command line

The o/p of this code on localhost:2000

Nodemon

⇒ Restarting a server every time after making a change is a pain.

⇒ This problem is solved by the library named Nodemon.

⇒ It automatically restarts the server as we save any changes in any file.

⇒ This increases the pace of coding and the programmers don’t have to restart the server manually and wait for the changes to compile up and run.

⇒ This is a very useful and popular tool used these days to auto restart the node server.

⇒ use the following command to install Nodemon

PS D:\work\Codebun\tutorial> npm i nodemon

And add the below line to the script of the package.json file created in Setup Express tutorial

 

"start":"nodemon app.js"

Then use the command npm start to automate the server.

PS D:\work\Codebun\tutorial> npm start