Our application at this point has a couple of issues:
- our front-end Angular application does not communicate with the server we built in assignment 1
- each time the server starts, we reload the data from the same JSON file.
It would be better if the data was stored elsewhere in a persistent fashion so that even if the server were to crash, our data would remain unharmed.
In this assignment, we will focus on persisting data by moving the JSON file to a dedicated database. In our case, we will be using MongoDB, which stores data as documents. These documents are very similar to JSON objects, making MongoDB a good candidate for our web application.
If you take a look at MongoDB’s introductory documentation, you will notice there’s quite a bit of code that has to be written to add, find, update, or delete data using their APIs. Mongoose simplifies the process of communicating with MongoDB and also provides tools to organize/model the data into schemas. Schemas are used to pre-define the data’s attributes, and the type each attribute will have. Read this tutorial that discusses how to use Mongoose with Node.js before continuing with the assignment.
MongoLab is an online service that will host your database in the cloud. While you could just run a local instance of MongoDB, registering for an account with MongoLab removes hassles surrounding installing the database on your machine. Make sure to register for an account before completing the assignment.
To port the listing data to MongoDB, you will be doing the following:
- Defining a schema for the ‘Listing’ collection
- Create a script that converts the JSON entries into Mongo documents and saves the documents to your database
- Retrieve/update/delete documents through database queries
- Clone the repository
- Define your schema in
- Update the
config.jsfile to include the uri to your MongoLab database.
- Fill out the
JSONtoMongo.jsscript to add the listings to your database
- Ensure your schema passes the tests by running
- Complete the functions in