In this video.
Let's go ahead and create a function to connect to MongoDB.
So inside the config, let's create one file called DB Connect dot JS.
So let's import the mongoose package and it's equal to required and mongoose.
So I'm going to be as Kong's DB connect.
So inside the function body I'm going to make use of try and catch.
And inside a try, I'm going to connect by making use of a and then mongoose dart connect.
And this function requires only one argument, and that is the connection string for MongoDB.
For the meantime, let's add some placeholder and call it as mongo.
You are ill and I'm going to show you how we're going to get the MongoDB.
You are error.
So in case everything goes right inside here, we are going to console.log by saying that DB connected.
And in case something goes wrong, we are going to console.log this by saying that.
And I want to add the exact error and we have access on the error message.
All right.
The last step is that I need to export this function.
So I'm going to be as module dot exports and is equal to DV connect.
But this how I would like to export this function.
And so this way I'm going to call the function automatically as DB connect.
So anywhere I require the file, this function is going to run automatically.
So let me remove this one.
So let's get back to the server.
That's where I want to call the DB connection and going to be as required.
And force last config and then DB connect and that is it.
So if I check the terminal and here you can see that we have beautiful error by saying that invalid
scheme meaning that we didn't provide the valid MongoDB you are error.
So the next video, let's see how we are going to create MongoDB connection in string.