BETAThis is a new service – your feedback will help us to improve it.

Using a database driver

Interacting with a database is difficult, and needs a lot of code.

It's easy to make mistakes that leave our app vulnerable to bugs and attacks.

Since nearly every web app needs a database and faces this problem, there's plenty of code already available that we can reuse.

Software that makes it easier for our app to talk to a database is called a database driver. We're going to use a popular driver called Mongoose.

Install Mongoose

The first step is to install Mongoose and record it as a dependency of our app, which we can do from the terminal.

Make sure the terminal is in your /my-app folder, then run:

npm install --save mongoose

Create a database user

We need to create a username and password for Mongoose to connect to our database with.

Go to mLab, and click on your database.

Then go to the Users tab and hit "Add database user".

Provide a new username and password for your database (not the same as your mLab account details) and finish creating the user.

Near the top of the page, you will see a connection sting URL, which looks like this:

mongodb://<dbuser>:<dbpassword>@ds151508.mlab.com:51508/my-app

Copy this string, replacing the <dbuser> and <dbpassword> with the database username and password we just created.

Configure Mongoose

Once Mongoose is installed, we need to make some changes to our server.js file, so that we connect to our database.

Add the following line to the top of the file:

const mongoose = require('mongoose')

Our app needs to make a connection to the database as soon as we start it.

Once that connection is made, we'll be able to run code that queries the database for data.

We can tell Mongoose to make a connection like this:

mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds151508.mlab.com:51508/my-app', { useNewUrlParser: true }, function(err){
  if(err) return console.log(err);
  console.log("DB connection opened");
})

Make sure that the connection string is the right string for your database, with <dbuser> and <dbpassword> replaced with the real username and password.

This block of code will try to make a connection, and will log an error to the terminal if it fails.

If the connection succeeds, we'll see the message "DB connection opened" instead.

Copy this block of code into your server.js file.

The modified file should look something like this:

const express = require('express')
const nunjucks = require('nunjucks')
const mongoose = require('mongoose')

let server = express()

nunjucks.configure(__dirname + '/views', {
  autoescape: true,
  express   : server
});

mongoose.connect('mongodb://<dbuser>:<dbpassword>@ds151508.mlab.com:51508/my-app', { useNewUrlParser: true }, function(err){
  if(err) return console.log(err);
  console.log("DB connection opened");
})

// Your routes here
// ...

server.use(express.static(__dirname + '/public'))

server.listen(3000, function(){
    console.log("Server is running on port 3000")
})
Once you've added the extra code, with the correct connection string, start your app to test the database connection. You should see the "DB connection opened" message.
Lessons last updated 12th July 2019. You can improve this lesson on Github.
Part of Databases
  1. Introducing databases
  2. Organising a databaseP
  3. Using a database driverP
  4. Creating modelsP
  5. Making forms workP
  6. Collecting form dataP
  7. Saving form dataP