Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
And now type npm install and then press Return. So now our project has all the modules it needs and is ready to go. Before connecting to MongoDB, you need to have a MongoDB database warmed up and ready to go. There are many options for doing this, but two I would like to point out. The first one is to install MongoDB on your computer, to do this, go to MongoDB.org and head to the downloads page. One thing to note though is that this doesn't come as a dot PKG file with a wizard based installer, instead you get some binary files which you then have to place in one of your executable paths.
The other option is to use a service like MongoLab. MongoLab will host a MongoDB database for you. Once you've signed up for MongoLab and created a database it will provide you with a connection string to use in your application. The downside is that the speed of data transfer will be slower than what it would be if you had a local MongoDB installation. I'll be using MongoLab here so I don't have to install MongoDB locally. I already have a MongoLab account, so I'm going to log in.
So now that I've logged into MongoLab, I can create a database. I'm going do this by clicking the create new button in the databases section. From here, you can select a cloud provider. I'm just gonig to leave it on Amazon web services, and then you can also pick a plan. If you go to the development tab, there's a free sandbox, this is going to be more than enough for what we'll do in this course. You can also select the MongoDB version. Finally, pick a name for your database. I'm going to call this database flights. Click create.
To get the connection string for this database, click on the database name. Since we haven't used this database yet, it's prompting us to add a username and a password. So let's do that. On this line, they're providing us with the string we need to use to connect. You'll want to use the database user and database password you entered on the last screen. Now, we're ready to connect to this database. In our application, we'll be using a node module called Mongoose. This module will let us connect to the database and make queries against it.
For the moment, we'll just install Mongoose and get connected. Let's install Mongoose now. Go back to terminal, and then type npm install dash, dash save mongoose. Once mongoose finishes installing, open up the code in your favorite text editor. I am going to add a file here called db.js. In this file I am going to do three things. First, I am going to require mongoose. Next, I'm going to add that connection string.
To do that I'm going to call mongoose.connect. Now copy the connection string and paste it into db.js, now supply the database user name and password that you entered. Remember this is not the same user name that you used to login to mongolab.com. Finally let's export the connection. To do that, I'm going to type module exports, and then I'm going to set it to mongoose.connection. Now let's make sure that this works. Go to server.js, and require db.js.
Finally, let's start up the server and see if any errors occur. No errors are occurring, so we should be connected to the database. Mongoose manages a connection between a MongoDB database and your appliction. You can install MongoDB locally or you can use a service like MongoLab to host your database for you. In the next video, we'll create a schema for flight data.
There are currently no FAQs about Node.js Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.