Setting up Mongoose and connecting to MongoDB

We will be using the Mongoose module to implement the user model in this skeleton, and also all future data models for our MERN applications. Here, we will start by configuring Mongoose, and utilizing it to define a connection with the MongoDB database.

First, to install the mongoose module, run the following command:

npm install mongoose --save

Then, update the server.js file to import the mongoose module, configure it to use native ES6 promises, and finally use it to handle the connection to the MongoDB database for the project.

mern-skeleton/server/server.js:

import mongoose from 'mongoose'mongoose.Promise = global.Promisemongoose.connect(config.mongoUri)mongoose.connection.on('error', () => ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.