So let's connect to mongodb
and for that, make sure you are on your clusters page so that you can click on connect there and find out
how to connect to this cluster.
Now we did connect through the shell in the past,
now we can also connect our application and for that, yes we are using the latest driver
and then you can just copy that address if you want.
You can also see the driver examples if you click on it down there,
so if you click on node here, you're basically forwarded to some part of the Atlas documentation in this
case where you see a code snippet on how you can connect to your address and you'll have to replace
some data there through the official driver
but we'll do this step by step.
So let's go back to the cluster,
copy that here and then head over to your code and in there,
let's start in the app.js file,
this is the file where basically all requests end up in which are sent to your node backend.
So whenever the frontend react sends some requests to the backend like get me all products,
first of all we start in this file, then in node express, the request is basically funneled through some
middleware as it's called,
so some functions that can interact on the incoming requests like set some headers here and then here,
I forward the requests to different routes, so different handlers you could say depending on whether I'm
requesting something related to products or just /nothing.
The routes are stored in the routes folder and for products in here, I have some dummy data but there, I have
a route for get requests
if it's just sent to /products, I just have slash here because the products filter is in the app.js file,
so if it's targeting /products, then this function is executed essentially and there, what I do is
I have some pagination logic in there which we can ignore for now,
I basically parse my dummy data and I return my data here,
so this is what I'm doing here. Now
app.js is also the file where the code is executed
when we start up our server, which we did with this npm run start server command. This basically just
use
nodejs,
basically we executed this script where we used nodejs to execute the app.js file and the app.js file
does some general setup like setting up all that middleware which will handle incoming requests and
therefore, we can connect to mongodb here in this file to automatically connect our app to the database
whenever we start the app server and we want to have a running connection so to say which can then do something
for incoming requests right from the start.
So here, the first thing we have to do is we have to import our mongo client, for this I'll create a new
constant,
so a new value which you will never change and then this is the import syntax in nodejs
and I will require mongodb, that is the package name of the package we installed
and there, the mongo client,
so this gives us access to the client which we need to establish a connection and so on. And what we can do now
at any point you want, maybe down there, you can use that mongodb constant and call connect
and this is also what you saw in that snippet
here. There you also see that constant or variable here was just named differently
but then I called connect and to connect, you pass that connection url and then you have a function
which is executed after the connection completed.
So here we can paste in that connection url which we got from the cluster page. Now important, you
will need a user name and for that user, you will also need a password,
so I'll just use that one, by the time you're viewing that, this will be offline
so using mine won't make much sense, then I'll use the Maximillian user, so I'll use that read and write
user here
and for this user, I just updated the user, the password and I'll paste the password in here too.
So now this is a url which allows the client to connect to the cluster,
I don't want to use the test database but I'll use a shop database which will be created on the fly
and with that, the connection will be established and now we could pass a function as a second argument
which will be executed when the connection finished or we use a feature called promises where we basically
have a then method to which we pass a function that will be executed once this connection finished
or we have a catch block that catches any potential errors.
Now here in the catch block, I'll simply output the error so that we can see it,
console log basically basically logs it to this console
so that we could see error messages here
and in then, I basically have an anonymous function and this is just how you create an anonymous function
in javascript that will execute
once we are connected.
So here I can say connected and here as an argument, I will actually get access to the client, so a client
which then allows me to execute a database method which then in turn allows me to for example work
with collections and so on.
So we'll do this later,
for now we can instantly close the client to remove the connection,
we'll of course later reuse it
but for now I'll immediately close it and just log connected in-between. Now after saving this and
after changing your server side code, you have to go to the terminal where you ran npm start server
and quit that process with control C and simply restart it because you changed something in your application,
now you need to restart it and after restarting, you should be able to connect to your mongodb database
server to the Atlas cluster.
Now with being connected, we took a first step but of course we're not doing anything useful here,
so in the next lectures, we'll of course start writing some code so that for example when we add new
products here, we store them in the database. That sounds like a good start so that we later are also
able to fetch them, doesn't it?