1 00:00:02,130 --> 00:00:03,740 So let's connect to mongodb 2 00:00:03,810 --> 00:00:11,340 and for that, make sure you are on your clusters page so that you can click on connect there and find out 3 00:00:11,340 --> 00:00:13,770 how to connect to this cluster. 4 00:00:13,770 --> 00:00:16,350 Now we did connect through the shell in the past, 5 00:00:16,350 --> 00:00:22,650 now we can also connect our application and for that, yes we are using the latest driver 6 00:00:22,980 --> 00:00:26,150 and then you can just copy that address if you want. 7 00:00:26,310 --> 00:00:30,430 You can also see the driver examples if you click on it down there, 8 00:00:30,600 --> 00:00:37,070 so if you click on node here, you're basically forwarded to some part of the Atlas documentation in this 9 00:00:37,090 --> 00:00:45,570 case where you see a code snippet on how you can connect to your address and you'll have to replace 10 00:00:45,570 --> 00:00:48,600 some data there through the official driver 11 00:00:48,690 --> 00:00:50,860 but we'll do this step by step. 12 00:00:50,880 --> 00:00:54,300 So let's go back to the cluster, 13 00:00:54,300 --> 00:00:58,850 copy that here and then head over to your code and in there, 14 00:00:58,860 --> 00:01:00,590 let's start in the app.js file, 15 00:01:00,630 --> 00:01:06,380 this is the file where basically all requests end up in which are sent to your node backend. 16 00:01:06,420 --> 00:01:12,630 So whenever the frontend react sends some requests to the backend like get me all products, 17 00:01:12,690 --> 00:01:18,450 first of all we start in this file, then in node express, the request is basically funneled through some 18 00:01:18,450 --> 00:01:19,870 middleware as it's called, 19 00:01:19,920 --> 00:01:25,410 so some functions that can interact on the incoming requests like set some headers here and then here, 20 00:01:25,600 --> 00:01:31,280 I forward the requests to different routes, so different handlers you could say depending on whether I'm 21 00:01:31,290 --> 00:01:35,450 requesting something related to products or just /nothing. 22 00:01:35,700 --> 00:01:41,340 The routes are stored in the routes folder and for products in here, I have some dummy data but there, I have 23 00:01:41,370 --> 00:01:43,280 a route for get requests 24 00:01:43,350 --> 00:01:49,960 if it's just sent to /products, I just have slash here because the products filter is in the app.js file, 25 00:01:49,970 --> 00:01:55,740 so if it's targeting /products, then this function is executed essentially and there, what I do is 26 00:01:55,890 --> 00:01:59,420 I have some pagination logic in there which we can ignore for now, 27 00:01:59,640 --> 00:02:04,210 I basically parse my dummy data and I return my data here, 28 00:02:04,230 --> 00:02:07,070 so this is what I'm doing here. Now 29 00:02:07,110 --> 00:02:11,070 app.js is also the file where the code is executed 30 00:02:11,070 --> 00:02:18,240 when we start up our server, which we did with this npm run start server command. This basically just 31 00:02:18,300 --> 00:02:18,980 use 32 00:02:19,050 --> 00:02:19,900 nodejs, 33 00:02:19,920 --> 00:02:26,980 basically we executed this script where we used nodejs to execute the app.js file and the app.js file 34 00:02:26,980 --> 00:02:31,980 does some general setup like setting up all that middleware which will handle incoming requests and 35 00:02:31,980 --> 00:02:39,150 therefore, we can connect to mongodb here in this file to automatically connect our app to the database 36 00:02:39,230 --> 00:02:45,630 whenever we start the app server and we want to have a running connection so to say which can then do something 37 00:02:45,630 --> 00:02:48,570 for incoming requests right from the start. 38 00:02:48,570 --> 00:02:55,780 So here, the first thing we have to do is we have to import our mongo client, for this I'll create a new 39 00:02:55,810 --> 00:02:56,650 constant, 40 00:02:56,680 --> 00:03:02,370 so a new value which you will never change and then this is the import syntax in nodejs 41 00:03:02,420 --> 00:03:07,700 and I will require mongodb, that is the package name of the package we installed 42 00:03:07,900 --> 00:03:09,600 and there, the mongo client, 43 00:03:09,640 --> 00:03:16,630 so this gives us access to the client which we need to establish a connection and so on. And what we can do now 44 00:03:17,810 --> 00:03:25,650 at any point you want, maybe down there, you can use that mongodb constant and call connect 45 00:03:25,890 --> 00:03:29,570 and this is also what you saw in that snippet 46 00:03:29,580 --> 00:03:34,950 here. There you also see that constant or variable here was just named differently 47 00:03:34,950 --> 00:03:40,470 but then I called connect and to connect, you pass that connection url and then you have a function 48 00:03:40,560 --> 00:03:43,800 which is executed after the connection completed. 49 00:03:43,800 --> 00:03:51,890 So here we can paste in that connection url which we got from the cluster page. Now important, you 50 00:03:51,890 --> 00:03:57,890 will need a user name and for that user, you will also need a password, 51 00:03:57,960 --> 00:04:02,010 so I'll just use that one, by the time you're viewing that, this will be offline 52 00:04:02,010 --> 00:04:10,350 so using mine won't make much sense, then I'll use the Maximillian user, so I'll use that read and write 53 00:04:10,410 --> 00:04:11,520 user here 54 00:04:11,730 --> 00:04:17,080 and for this user, I just updated the user, the password and I'll paste the password in here too. 55 00:04:17,340 --> 00:04:22,710 So now this is a url which allows the client to connect to the cluster, 56 00:04:22,740 --> 00:04:27,790 I don't want to use the test database but I'll use a shop database which will be created on the fly 57 00:04:28,140 --> 00:04:35,700 and with that, the connection will be established and now we could pass a function as a second argument 58 00:04:35,790 --> 00:04:43,380 which will be executed when the connection finished or we use a feature called promises where we basically 59 00:04:43,380 --> 00:04:49,230 have a then method to which we pass a function that will be executed once this connection finished 60 00:04:49,710 --> 00:04:53,860 or we have a catch block that catches any potential errors. 61 00:04:54,150 --> 00:04:57,870 Now here in the catch block, I'll simply output the error so that we can see it, 62 00:04:58,050 --> 00:05:01,160 console log basically basically logs it to this console 63 00:05:01,200 --> 00:05:03,100 so that we could see error messages here 64 00:05:03,600 --> 00:05:10,140 and in then, I basically have an anonymous function and this is just how you create an anonymous function 65 00:05:10,260 --> 00:05:12,620 in javascript that will execute 66 00:05:12,660 --> 00:05:14,210 once we are connected. 67 00:05:14,340 --> 00:05:23,460 So here I can say connected and here as an argument, I will actually get access to the client, so a client 68 00:05:23,460 --> 00:05:28,650 which then allows me to execute a database method which then in turn allows me to for example work 69 00:05:28,830 --> 00:05:30,590 with collections and so on. 70 00:05:30,600 --> 00:05:32,370 So we'll do this later, 71 00:05:32,520 --> 00:05:36,290 for now we can instantly close the client to remove the connection, 72 00:05:36,300 --> 00:05:37,830 we'll of course later reuse it 73 00:05:37,830 --> 00:05:43,440 but for now I'll immediately close it and just log connected in-between. Now after saving this and 74 00:05:43,500 --> 00:05:49,590 after changing your server side code, you have to go to the terminal where you ran npm start server 75 00:05:50,010 --> 00:05:58,280 and quit that process with control C and simply restart it because you changed something in your application, 76 00:05:58,290 --> 00:06:06,510 now you need to restart it and after restarting, you should be able to connect to your mongodb database 77 00:06:06,510 --> 00:06:09,040 server to the Atlas cluster. 78 00:06:09,060 --> 00:06:15,470 Now with being connected, we took a first step but of course we're not doing anything useful here, 79 00:06:15,480 --> 00:06:21,900 so in the next lectures, we'll of course start writing some code so that for example when we add new 80 00:06:21,900 --> 00:06:27,470 products here, we store them in the database. That sounds like a good start so that we later are also 81 00:06:27,480 --> 00:06:29,130 able to fetch them, doesn't it?