Configuring Mongoose

This is where the best part starts. So, we will need to create a connection to the database; to do this, open the mongoose-connection.js file and apply the following changes:

const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost:32768/wcDb')

mongoose.Promise = global.Promise

mongoose.connection.on('connected', () => {
console.log('connection is ready')
})

mongoose.connection.on('error', () => {
console.log(err)
})

First, we import the Mongoose module and host it into the mongoose constant. Then, we call the connect function and pass the connection URL using the host and port that points to our MongoDB docker container. Lastly, we tell mongoose that our database name will be wcDb. If the connection was successful, the connected event will be called and the connection is ready message should be printed. Let's test things out; execute the following command:

$ node src/config/mongoose-connection.js

connection is ready

Cool! Our Node.js module is able to establish a successful connection with MongoDB using Mongoose. Now we will need to define schemas, models, and collections. Keep reading!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset