Looks like my cluster has now been created.
So now we're gonna make sure that we connect to it from our Express application.
To do so we'll find the Connect button here on the left hand side and we're going to go through a little
bit more setup.
The first thing we have to do is whitelist our connection IP address so whenever we create a mongo server
through Mongo DB dot com we can only connect to the Mongo instance from one of a set of white listed
IP addresses.
If you click on the green button that says Add Your current IP address Mongo is going to figure out
what your current IP address is and put it into this little input right here.
Now if you change your Internet connection at any point time in the next couple of days like let's say
you go from a company Wi-Fi network to your home Wi-Fi network or if you go to a coffee shop or whatever
else your IP address is going to be different.
And that means that your IP address needs to be added onto this whitelist.
So in this case I'm gonna add my current IP address and if I need to add any more I can click on the
link right there to add in some additional IP addresses you'll probably want to do that.
Like I said if you change your Wi-Fi network at any point time in the next couple of days as you're
working on this application after that I'm going to create a mongo DV user.
Now this is a account that is tied to your actual database.
It's not tied to like Mongo DB Atlas which is a service that we're currently using.
So for your username and password here you can add in essentially anything you want to add in for your
username and password.
But I recommend that you make something that you can very easily remember.
So for me I'm going to use a user name of admin and a password of password password and then I'll click
on Create Mongo DB user
after that I click on choose a connection method and we're going to look for the option out of these
three that says something about a connection string.
So for me that's the second option right here.
That's going to then show me a connection string and that's that long thing right there.
It's going to click on copy.
I'm going to go back over to my code editor and I'm going to paste in that string right after we create
our app right there.
I'm going to put it inside of a set of quotes and then I'll sign it to a variable that I'll call Mongo.
You are I like so then inside the string you'll notice that there's kind of a blank right here for our
password just to the left that you'll see whatever user name you just entered.
So for me it's admin then a colon.
And then what is supposed to be our password.
So I gotta delete the password that's in there.
So I have now a colon and then a at symbol in between those two.
I'll put in the password that I just entered into that UI.
So for me it was password password like so.
OK so now we can use this Mongo you are right here to actually connect to our Mongo instance now before
I do just one more time.
I want to remind you that if you change your Wi-Fi network at any point time in the next couple of days
as you're still working on this application you will not be able to connect it to your Mongo cluster
anymore.
So if you change your Wi-Fi network you're going to go to this left hand menu over here find a network
access and then click on Add IP address if you want to.
You can always just allow access from anywhere.
And that's going to essentially allow any IP address to access your database.
Not ideal but since this is really just a learning opportunity here you can always do that if you want
to.
All right.
So back over here and sign my code.
Ed we've now got the string that we need to actually connect to our Mongo instance remember to actually
connect to mongo.
We're going to use that library called Mongoose.
So I'm going to import Mongoose at the very top.
I'll say const mongoose is require Mongoose and then right after we create are Mongo you or I.
I'll then call Mongoose dot connect and we'll pass in the Mongo you or I as the first argument then
as a secondary argument we'll put in an options object inside of here we're gonna say use new you are
L parser true and use create index true as well.
These two settings right here are really not that interesting.
Essentially we're just adding them in to prevent some common error messages and warnings from appearing
inside of our terminal then right after that we're gonna add in a little bit of code to just make sure
that we successfully connect to our Mongo instance and we can also detect any errors that occur as we're
connecting.
So we're gonna call Mongoose dot connection dot on all then put in as a first argument a string of connected
and a callback function.
So now anytime we successfully connect to our Mongoose instance this callback function right here is
going to be executed.
So I'm just gonna put a little console log in there that says something like connected to mongo instance.
So with that I'll know when I actually successfully connect to my instance and I'll do the same thing
to handle any possible error that might arise as well.
So I'll say Mongoose loops Mongoose dot connection dot on air
this callback right here.
Whenever there is an error we'll be called with some air object so I can receive that era object and
I'll use it inside of a console dot air and I'll say something like air connecting to mongo and then
as a second argument I'll put in that error argument that I just received.
OK so now we've got some code here to connect to mongo we know when we connect successfully and we'll
also know when there's some kind of error as well.
So let's save this and try restarting our server and make sure we can actually connect.
So save the file I'll fly back over to my terminal and then I'm going to hit control C to stop the running
server and I'll start it back up with node source index dot J.S. It looks like in this case I've got
some kind of error that occurred.
So what's going on here.
Looks like I've got a invalid connection string.
I think I know what went wrong here.
If I go back over it looks like my copy paste included a little space between Mongo D.B. the plus and
the SRB and it really should be just one continuous string.
So gonna delete those extra spaces that were for some reason added in and if I try this once again yeah.
There we go.
That's better.
So now I'm listening on port three thousand enough connected to my Mongo instance.
Perfect gates and now we've got Mongo support added in.
Let's take another quick pause right here.
When we come back the next video we're going start setting up authentication inside of our app.