So now that we added our API routes
or the newsletter in for the comments
and that we wired up
those front-end components for the newsletter
and for the comments.
Now that all of that was done.
It's time to work on the logic inside of the API routes
because at the moment we don't do anything in there.
In the newsletter API route we're
just logging the entered email
and in the comments API route
for the post case,
we also in the end,
just log our comment here.
And that of course is not too helpful.
That's not the goal.
Instead, we wanna store that data more permanently
and we could of course use a file for that.
We could re-add a data folder
in our project with some JSON
or a text file and write that data to that file.
But I would argue it's more realistic to use a database
and therefore that's what we'll do.
I'll use a real database here.
I'll use MongoDB.
MongoDB of course is just one option.
It's not a must use option,
but I will use it because using MongoDB
with Node.js is particularly easy
and it's also a production ready,
scalable database which is a great choice
for pretty much all projects.
For a lot of projects, Let's put it like this.
And that's why we'll use MongoDB.
Even better than that, we can get started for free.
If you visit MongoDB.com and you go to pricing,
you'll see that they have various offerings
and we will use the cloud service here,
MongoDB Atlas.
That is their fully managed cloud database service,
which we can use without setting up anything
on our local system, which is great.
And we can get started for free here,
which is awesome.
So we can click on start free here actually.
Or in the top right corner,
click on Try Free,
click on Cloud
and then get started for free here by signing up.
just fill out these basic details and sign up.
I already do have an account
so I will log in with that.
And once you did sign up and log in,
you should be greeted with some screen
that looks something like this.
You should be able to build a cluster
and then there use a shared cluster.
So, create a cluster here
where you can get started for free.
No, a cluster is basically a server.
You could say,
which will then later host your databases,
you could say.
Now here we need to choose a cloud provider
which will be used under the hood.
And we can stick to AWS here
and basically keep the defaults
for a cluster tier let's stick
to the M0 Sandbox since that's available for free.
And for the additional settings,
we also can stick to the defaults
and we can also keep that default cluster name
and create that cluster.
Now, this will set up this database cluster
which will then enable us to connect
to this cluster and store data
in the databases there.
And using it will be super simple.
Now, whilst this is getting set up,
we can go back to our project already
and quit the dev server
and install an extra package
with npm install.
Because in order to communicate
with MongoDB and to send queries to MongoDB,
we need to install a new third party package.
If you search for Node.js MongoDB,
you also find it on npm.
It's this MongoDB package
which is the official driver for MongoDB.
So that makes connecting and talking
to the Mongo DB database super easy then.
So we should install MongoDB,
this MongoDB package here like this
with npm installed MongoDB.
And this will install this third party package
which we'll then use in our server side code
to talk to MongoDB to our cluster
and to store data and
to fetch data as well Of course.
And the important thing here really is
that we'll use it in our server side code.
So in these API routes,
we will not use this Mongo DB package
in any client side code.
A, because it's not optimized for that.
It might not even work there because there
is a high chance that it uses Node.js APIs internally
and b, even if it does work in the browser,
it would be a security issue.
Because for talking to the database,
we will need to plug our credentials
into the code and we don't wanna do
that in any code our users can see
because they could then abuse those credentials
to hack into our database.
That's why we want to run such database code only
on the server side.
So, only in our API routes.
Now connecting to the cluster is done here now.
And when we clicked on Connect,
we can choose connect your application.
And we see code which we can use to connect
to our MongoDB cluster with the Node.js driver here.
And that's what we're going
to do in the next lectures.