1
00:00:00,020 --> 00:00:00,350
All right.

2
00:00:00,350 --> 00:00:02,990
And up next, let's connect to our database.

3
00:00:03,020 --> 00:00:06,890
Now, in order to do that, we need to import mongoose from Mongoose.

4
00:00:06,920 --> 00:00:10,150
It has a method by the name of Connect.

5
00:00:10,160 --> 00:00:14,590
So we go here with Mongoose Dot Connect, and we need to provide that connection string.

6
00:00:14,600 --> 00:00:21,530
Remember the variable we set up a few videos ago and since it's located in dotenv we want to go with

7
00:00:21,530 --> 00:00:25,010
process dot env and then mongo URL.

8
00:00:25,040 --> 00:00:33,080
And it's always my preference to essentially wrap everything in try and catch and right after the mongoose

9
00:00:33,080 --> 00:00:37,460
connect, which returns a promise and that's why we stick await in front of it.

10
00:00:37,520 --> 00:00:40,580
And remember we don't need to create a function anymore.

11
00:00:40,700 --> 00:00:45,980
Now we can just do it without the function we can simply set up await.

12
00:00:45,980 --> 00:00:51,740
And right after that I'll set up my app.listen and all of this is going to be wrapped in try and catch

13
00:00:51,740 --> 00:00:54,680
so that way if there's any issue.

14
00:00:54,680 --> 00:01:00,840
So if I cannot connect to the database, well, there's really no point for my server, correct, since

15
00:01:00,840 --> 00:01:05,430
all of the functionality involves connecting to the database, Hopefully that is clear.

16
00:01:05,430 --> 00:01:10,560
So therefore here in the catch we just go with console log and then we go process dot exit and we pass

17
00:01:10,560 --> 00:01:13,730
in one which stands basically for error.

18
00:01:13,740 --> 00:01:16,680
So now let's navigate to a server like always.

19
00:01:16,680 --> 00:01:19,080
We want to start by importing stuff.

20
00:01:19,080 --> 00:01:23,850
So we're going to go here, Mongoose from Mongoose, and let's keep on moving.

21
00:01:24,090 --> 00:01:26,430
Or here we have the port, Beautiful.

22
00:01:26,430 --> 00:01:29,220
Right after that we'll set up the try and catch.

23
00:01:29,850 --> 00:01:33,780
Like I already said few times, we can right away use the await.

24
00:01:34,420 --> 00:01:39,970
Nothing stops us and then we want to go with process dot env.

25
00:01:41,360 --> 00:01:43,160
And we're looking for Mongo.

26
00:01:44,220 --> 00:01:49,310
Underscore you URL and then we'll move this sucker a little bit up.

27
00:01:49,320 --> 00:01:51,360
So if we're successful.

28
00:01:51,360 --> 00:01:51,990
Yep.

29
00:01:51,990 --> 00:01:54,120
Then let's start our server.

30
00:01:54,150 --> 00:01:57,510
Now, if there's some kind of error, we most likely want to see it.

31
00:01:57,510 --> 00:02:03,840
So we'll go here with console log error and then we're going to go with process dot exit.

32
00:02:04,350 --> 00:02:08,449
So if there is an error, we want to exit and we want to provide one.

33
00:02:08,460 --> 00:02:13,230
So now let's open up our console and let's spin up our server.

34
00:02:13,230 --> 00:02:19,080
So we're going to go over here with NPM run dev and if everything is correct, we'll see over here.

35
00:02:19,080 --> 00:02:22,740
Server running on port 5100.

36
00:02:22,740 --> 00:02:26,250
So that means that we successfully connected to the database.

37
00:02:26,370 --> 00:02:33,330
Now before we move on, let me just show you a typical error you might get if, let's say your password

38
00:02:33,330 --> 00:02:34,020
is wrong.

39
00:02:34,380 --> 00:02:35,420
Let me go here.

40
00:02:35,430 --> 00:02:37,290
Let me remove one character.

41
00:02:37,920 --> 00:02:41,250
Remember, we do need to pretty much restart everything.

42
00:02:41,340 --> 00:02:46,410
And now you'll see a big, massive, giant error in the console.

43
00:02:46,440 --> 00:02:48,630
Basically, we have bad auth.

44
00:02:48,840 --> 00:02:54,420
And again, if you see this type of error and notice it even says mongo server error and all that,

45
00:02:54,480 --> 00:02:58,020
that means you need to double check your connection string.

46
00:02:58,020 --> 00:03:00,450
So again, let me go back over here.

47
00:03:02,010 --> 00:03:05,010
Then let me restart NPM run.

48
00:03:05,220 --> 00:03:12,000
So if you don't see any errors, you're in good shape and we can move on to the next step.

