Put the MongoDB URO in the .env file

MONGO_URI = mongodb://localhost:27017/quoteGenerator

If you are using Mongo Atlas instead of Mongo local, you can replace the URI with it

Make sure you restart the server once you make changes to the .env file

Create config folder inside the backend folder

Create db.js file to connect with the MongoDB database

Bring in Mongoose into the db.js file

const mongoose = require("mongoose");

Bring in colors package into the server.js file in order to see the console texts in a specified color

const colors = require("colors");

Write the function to connect to the MongoDB database

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(process.env.MONGO_URI);

    console.log(`MongoDB Connected: ${conn.connection.host}`.yellow.underline);
  } catch (error) {

Export the function

module.exports = connectDB;

Bring in the function to connect the database into server.js

const connectDB = require("./config/db");

Initialize the function


Run the development server in order to connect to the database

npm run dev