1 00:00:01,050 --> 00:00:02,790 Looks like my cluster has now been created. 2 00:00:02,790 --> 00:00:06,330 So now we're gonna make sure that we connect to it from our Express application. 3 00:00:06,330 --> 00:00:10,290 To do so we'll find the Connect button here on the left hand side and we're going to go through a little 4 00:00:10,290 --> 00:00:11,380 bit more setup. 5 00:00:11,520 --> 00:00:16,590 The first thing we have to do is whitelist our connection IP address so whenever we create a mongo server 6 00:00:16,620 --> 00:00:22,920 through Mongo DB dot com we can only connect to the Mongo instance from one of a set of white listed 7 00:00:22,980 --> 00:00:24,720 IP addresses. 8 00:00:24,900 --> 00:00:29,040 If you click on the green button that says Add Your current IP address Mongo is going to figure out 9 00:00:29,070 --> 00:00:33,680 what your current IP address is and put it into this little input right here. 10 00:00:33,690 --> 00:00:38,700 Now if you change your Internet connection at any point time in the next couple of days like let's say 11 00:00:38,700 --> 00:00:44,250 you go from a company Wi-Fi network to your home Wi-Fi network or if you go to a coffee shop or whatever 12 00:00:44,250 --> 00:00:47,360 else your IP address is going to be different. 13 00:00:47,580 --> 00:00:52,650 And that means that your IP address needs to be added onto this whitelist. 14 00:00:52,650 --> 00:00:57,210 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 15 00:00:57,210 --> 00:01:02,070 link right there to add in some additional IP addresses you'll probably want to do that. 16 00:01:02,070 --> 00:01:06,690 Like I said if you change your Wi-Fi network at any point time in the next couple of days as you're 17 00:01:06,690 --> 00:01:12,220 working on this application after that I'm going to create a mongo DV user. 18 00:01:12,220 --> 00:01:16,090 Now this is a account that is tied to your actual database. 19 00:01:16,120 --> 00:01:21,020 It's not tied to like Mongo DB Atlas which is a service that we're currently using. 20 00:01:21,070 --> 00:01:25,870 So for your username and password here you can add in essentially anything you want to add in for your 21 00:01:25,870 --> 00:01:26,980 username and password. 22 00:01:27,040 --> 00:01:29,830 But I recommend that you make something that you can very easily remember. 23 00:01:30,430 --> 00:01:37,120 So for me I'm going to use a user name of admin and a password of password password and then I'll click 24 00:01:37,120 --> 00:01:38,680 on Create Mongo DB user 25 00:01:41,440 --> 00:01:46,980 after that I click on choose a connection method and we're going to look for the option out of these 26 00:01:46,980 --> 00:01:49,440 three that says something about a connection string. 27 00:01:49,500 --> 00:01:52,310 So for me that's the second option right here. 28 00:01:54,010 --> 00:01:58,490 That's going to then show me a connection string and that's that long thing right there. 29 00:01:58,570 --> 00:02:00,070 It's going to click on copy. 30 00:02:00,270 --> 00:02:05,840 I'm going to go back over to my code editor and I'm going to paste in that string right after we create 31 00:02:05,900 --> 00:02:07,180 our app right there. 32 00:02:08,730 --> 00:02:14,780 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. 33 00:02:14,790 --> 00:02:21,220 You are I like so then inside the string you'll notice that there's kind of a blank right here for our 34 00:02:21,220 --> 00:02:25,120 password just to the left that you'll see whatever user name you just entered. 35 00:02:25,120 --> 00:02:27,510 So for me it's admin then a colon. 36 00:02:27,520 --> 00:02:29,670 And then what is supposed to be our password. 37 00:02:29,770 --> 00:02:33,190 So I gotta delete the password that's in there. 38 00:02:33,190 --> 00:02:37,770 So I have now a colon and then a at symbol in between those two. 39 00:02:37,780 --> 00:02:40,400 I'll put in the password that I just entered into that UI. 40 00:02:40,690 --> 00:02:44,770 So for me it was password password like so. 41 00:02:44,880 --> 00:02:50,490 OK so now we can use this Mongo you are right here to actually connect to our Mongo instance now before 42 00:02:50,490 --> 00:02:51,770 I do just one more time. 43 00:02:51,780 --> 00:02:56,130 I want to remind you that if you change your Wi-Fi network at any point time in the next couple of days 44 00:02:56,160 --> 00:03:00,480 as you're still working on this application you will not be able to connect it to your Mongo cluster 45 00:03:00,480 --> 00:03:01,530 anymore. 46 00:03:01,590 --> 00:03:06,690 So if you change your Wi-Fi network you're going to go to this left hand menu over here find a network 47 00:03:06,690 --> 00:03:12,030 access and then click on Add IP address if you want to. 48 00:03:12,030 --> 00:03:14,230 You can always just allow access from anywhere. 49 00:03:14,250 --> 00:03:18,660 And that's going to essentially allow any IP address to access your database. 50 00:03:18,660 --> 00:03:23,310 Not ideal but since this is really just a learning opportunity here you can always do that if you want 51 00:03:23,310 --> 00:03:25,730 to. 52 00:03:25,730 --> 00:03:26,460 All right. 53 00:03:26,510 --> 00:03:28,060 So back over here and sign my code. 54 00:03:28,060 --> 00:03:32,780 Ed we've now got the string that we need to actually connect to our Mongo instance remember to actually 55 00:03:32,780 --> 00:03:33,610 connect to mongo. 56 00:03:33,620 --> 00:03:36,260 We're going to use that library called Mongoose. 57 00:03:36,260 --> 00:03:38,760 So I'm going to import Mongoose at the very top. 58 00:03:38,930 --> 00:03:47,250 I'll say const mongoose is require Mongoose and then right after we create are Mongo you or I. 59 00:03:47,730 --> 00:03:56,000 I'll then call Mongoose dot connect and we'll pass in the Mongo you or I as the first argument then 60 00:03:56,030 --> 00:04:02,210 as a secondary argument we'll put in an options object inside of here we're gonna say use new you are 61 00:04:02,210 --> 00:04:09,070 L parser true and use create index true as well. 62 00:04:09,250 --> 00:04:12,490 These two settings right here are really not that interesting. 63 00:04:12,490 --> 00:04:16,870 Essentially we're just adding them in to prevent some common error messages and warnings from appearing 64 00:04:16,870 --> 00:04:22,120 inside of our terminal then right after that we're gonna add in a little bit of code to just make sure 65 00:04:22,120 --> 00:04:27,670 that we successfully connect to our Mongo instance and we can also detect any errors that occur as we're 66 00:04:27,670 --> 00:04:28,770 connecting. 67 00:04:28,870 --> 00:04:37,120 So we're gonna call Mongoose dot connection dot on all then put in as a first argument a string of connected 68 00:04:37,570 --> 00:04:39,750 and a callback function. 69 00:04:39,750 --> 00:04:44,080 So now anytime we successfully connect to our Mongoose instance this callback function right here is 70 00:04:44,080 --> 00:04:45,350 going to be executed. 71 00:04:45,400 --> 00:04:53,600 So I'm just gonna put a little console log in there that says something like connected to mongo instance. 72 00:04:53,610 --> 00:04:58,200 So with that I'll know when I actually successfully connect to my instance and I'll do the same thing 73 00:04:58,200 --> 00:05:01,120 to handle any possible error that might arise as well. 74 00:05:01,290 --> 00:05:06,570 So I'll say Mongoose loops Mongoose dot connection dot on air 75 00:05:09,490 --> 00:05:10,670 this callback right here. 76 00:05:10,690 --> 00:05:16,300 Whenever there is an error we'll be called with some air object so I can receive that era object and 77 00:05:16,300 --> 00:05:24,310 I'll use it inside of a console dot air and I'll say something like air connecting to mongo and then 78 00:05:24,310 --> 00:05:30,130 as a second argument I'll put in that error argument that I just received. 79 00:05:30,160 --> 00:05:35,070 OK so now we've got some code here to connect to mongo we know when we connect successfully and we'll 80 00:05:35,070 --> 00:05:37,050 also know when there's some kind of error as well. 81 00:05:37,520 --> 00:05:41,540 So let's save this and try restarting our server and make sure we can actually connect. 82 00:05:41,700 --> 00:05:48,420 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 83 00:05:48,420 --> 00:05:55,650 server and I'll start it back up with node source index dot J.S. It looks like in this case I've got 84 00:05:55,650 --> 00:05:56,980 some kind of error that occurred. 85 00:05:57,000 --> 00:05:58,130 So what's going on here. 86 00:05:59,200 --> 00:06:01,870 Looks like I've got a invalid connection string. 87 00:06:01,870 --> 00:06:03,700 I think I know what went wrong here. 88 00:06:03,790 --> 00:06:10,330 If I go back over it looks like my copy paste included a little space between Mongo D.B. the plus and 89 00:06:10,330 --> 00:06:13,750 the SRB and it really should be just one continuous string. 90 00:06:13,750 --> 00:06:22,080 So gonna delete those extra spaces that were for some reason added in and if I try this once again yeah. 91 00:06:22,090 --> 00:06:22,440 There we go. 92 00:06:22,450 --> 00:06:23,620 That's better. 93 00:06:23,640 --> 00:06:27,580 So now I'm listening on port three thousand enough connected to my Mongo instance. 94 00:06:27,700 --> 00:06:30,970 Perfect gates and now we've got Mongo support added in. 95 00:06:30,970 --> 00:06:32,340 Let's take another quick pause right here. 96 00:06:32,350 --> 00:06:35,890 When we come back the next video we're going start setting up authentication inside of our app.