- [Instructor] In this video, we will now add Mongoose
to our project and we will also set it up
to establish the connection to MongoDB.
For that I already have opened Visual Studio code
and in the terminal, I'm in the shopper directory.
So just make sure that you're in the right directory.
So I move out again, cd ..
and you have to do a cd shopper
to really be in the shopper directory.
And I'm now running npm install mongoose.
And this will add Mongoose to the project
and it will also install all sub-dependencies
and one sub-dependency
of Mongoose is our MongoDB driver that we used
in the previous example because under the hood,
Mongoose is relying on this driver as well.
The next question is, where should we add the code
that establishes the connection to MongoDB?
And I would argue
that the express server should not start at all
if we cannot connect to the database,
because what's the point
if the database is not there, there are no items
in the shop and this should be a critical error
and the application should not start.
To accomplish that,
I will now open shopper,
server, bin, start.js,
because here, if you scroll down,
we are starting the server in the last line
by calling server.listen.
To bring in Mongoose here,
I will scroll up again and add after a required HTTP,
I will now require Mongoose.
So add const mongoose equals require mongoose.
Then right after I brought in the config
and instantiated the app.
So after line seven, I add a new function
and this function is asynchronous,
async function connectToMongoose.
And in there, I will now return
and I will call mongoose.connect.
And you see that IntelliSense is here helping us.
It already suggests the connect method
and this connect method takes arguments of course.
And the first argument is the urlToMongodb.
I could now of course just hard-code it here,
but I prefer to have all configuration at the central place
and config, index.js is the right place for that.
So here in config index.js,
I will now export a new property called mongodb.
And in there I define a sub property url
and this should be mongodb,
localhost,
then port 37017.
So that's again, the MongoDB running in a Docker container.
If you don't have it running right now,
make sure that it is running.
And the database we want to use is called shopper.
If this database does not exist already,
MongoDB will take care of creating it.
So you don't have to do anything in MongoDB directly here.
This will just work out of the box.
Now I'm heading back to start.js.
And now I can just use this config object
that I require in line six and add config, mongodb.url.
And in there I will now add again a few feature targets
that make sure
that I'm using new language features
and this avoid deprecation warnings
that you would get on the console otherwise.
So add, use newUrlParser
and you see IntelliSense here helps us
and already suggests that, this is true.
Use unified typology.
That's true as well and use create index.
That's true as well.
If you're curious about what each of them does,
just look them up.
So it's just really to use the newest features
of MongoDB and avoid warnings.
As I said before, I want the server to only start
when this connection was successful.
So let's scroll now down,
and right before I call the server listen,
I now call this function, connectToMongoose.
And as you saw,
this is an asynchronous function and it returns a promise.
And if it returns a promise, I can call then on the result.
And in there, I will first just log out console.info
and let's say, successfully connected to MongoDB.
Every time you have the then block,
you also need the catch block,
that is called if something went wrong.
So add catch error here, and I log out console.error
and I just log out the error that I get
from the exception that was wrong before.
Next, as I said,
I want the server to start up only if this succeeded.
So in the then block,
I will now copy the server listen statement.
Okay, we are now ready to try this out.
And we talked about that before.
In the package json, I defined a script dev
that uses nodemon to start the server.
So I can now run npm run dev,
and we see that,
we first successfully connected to MongoDB
and then the express server starts listening on port 3000.