How to connect with database locally with mongo DB | MERN

In this tutorial, we will learn how to connect databases locally with mongo DB . if you want to learn new things in react do comment in the post which post you would like to learn. So without wasting any time let’s start the how to connect with the database locally with mongo DB | MERN.

Let’s start with the first step to connect with mongo DB locally.

First, we have to pass the process file.

const mongoose = require("mongoose");
require("colors");

//connectDB function // this function is basically used to connect with the data base and the make a connection with the data base locaally
const connectDb = async () => {

    try {
    const conn = await mongoose.connect(process.env.MONGO_URI);
    console.log(`MongoDB Connected ${conn.connection.host}`.bgYellow);
    } 
    catch (error) {
        console.log(`Error : ${error.message}`.bgRed);
        process.exit(1);
    }

};


module.exports = connectDb;

Conclusion

So there we have it. If you want to easily connect your database do follow these above steps. To learn more about mongo DB and other tutorials do comment on this

ziontutorial

Back to top