Step 4
Before connecting the YouNote API to the MongoDB cluster in the cloud, we will make a demo app to get familiar and practice working with MongoDB from a Node application.
Create a folder mongo-demo
. Open the terminal and change directory to mongo-demo
. Initiate a Node application:
npm init -y
There is an official MongoDB driver for NodeJS. It provides an API to work with MongoDB from within Node applications. We, however, are going to use another Node package called Mongoose to work with MongoDB.
Mongoose is an object document mapping (ODM) that sits on top of Node's MongoDB driver.
It is not required to use Mongoose with MongoDB but there are advantages in doing so (see e.g. Top 4 reasons to Use Mongoose with MongoDB).
Install mongoose and while at it, install faker as well so we can populate our database with some fake data!
npm install mongoose faker
Create an index.js
file:
const mongoose = require("mongoose");
console.log(mongoose);
Save the file and run it
node index.js
Notice the mongoose
object printed to the terminal. We will be using some of the functions attached to this object.