Building an e-Commerce website with Node – Part 09 – Exporting Schema, installing mongoose and bcrypt, configuring mongoDB database

We have created the user schema, now before going further, we have to export it, so other files can use it. At the end of the user.js file add the following code:


module.exports = mongoose.model('User', UserSchema);

Next, we have to check the package.json file to see if we have mongoose and bcrypt installed. If not, go ahead and install it. Open a terminal and type the following command:

npm install mongoose bcrypt-nodejs --save

Install mongoose and mongoDB

Now, check again the package.json file. Mongoose and bcrypt are listed as installed.

Now we need to go to the server.js and require the mongoose library there too. The server.js needs to use this library too.


var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

It is the same as we required the mongoose library in user.js.

Next, we need to connect mongoose to the database. In the server.js file, above the middleware section type in the following code:


mongoose.connect('', function(err){
  if (err) {
    console.log(err);
  } else {
    console.log("Connected to the database");
  }
});

mongoose.connect() is a method of mongoose library. Between parentheses, we normally should type in the path to the database but since we haven’t set it up yet, we just have an empty string now.

The second parameter is an anonymous function that checks for errors and validates.

We need to create that database and get the path to the database for our code. Go to mlab.com website and create an account. Once you are done with that you can see a button that says “Create new”. Click it to create a new MongoDB database.

Create a new mongoDB deployment

Now you can choose a cloud provider (I have chosen Amazon), go to “Single-node” tab, choose “sandbox” (it is free), at the database name field I named it wpmyeshop, and finally click “Create new mongoDB deployment” button.

Now the database is created but we have to create a user for the database (there is a yellow message on the screen). Go to “Users” tab and click the button “Add database user”. Type the username, the passoword, the password confirmation and you are all set.

On top of the page you will see something that says “To connect using a driver via the standard MongoDB URI” and below it there is a link (in my case the link is: mongodb://:@ds161021.mlab.com:61021/wpmyeshop). Copy the link and paste it into the server.js file as is shown below:


mongoose.connect('mongodb://:@ds161021.mlab.com:61021/wpmyeshop', function(err){
  if (err) {
    console.log(err);
  } else {
    console.log("Connected to the database");
  }
});

Now, we have to replace dbuser and dbpassword with the actual user and password of the user we have just created. In my case the user is root and the password is 1234. So, what you actually should have is:


mongoose.connect('mongodb://root:1234@ds161021.mlab.com:61021/wpmyeshop', function(err){
  if (err) {
    console.log(err);
  } else {
    console.log("Connected to the database");
  }
});

Now go back to the terminal and start the server to check if “connected to the database” message will be shown.connect to mongoDB

Everything is fine, we successfully connected our Node.js server to the mongoDB database.

Leave a Reply