Code Snippets: Storing Data with Mongo

Now that I have schemas in my models folder for both users and snippets I'm going to need a way to store them. I could use a SQL database which would store the snippets in the user table with a foreign key or a NoSQL database which is a bit more flexible. Even though SQL is the better choice, I am more familiar with Mongoose then Sequelize (both are the middleware used to help Node talk to the database).  For now, I will use Mongo and Mongoose to store the data from my application.

Since I put each of those schemas in their own file I was able to export them. Once they were exported, I could require them in my main file along with the routes I created for each of my pages. I made a route for the login, the register page, the home page, and a place to render the results of a search. All of these are referenced in my app.js - the index of my project. In that same file is where I declare a url for Mongoose. Mongoose needs a connection between my application and the database inside of Mongo so I'm able to to use a localhost and the name of the database I want to store the data into. Once I've done that, I nest my listen inside of my connection!


I added a console.log to make sure I knew this worked and it did! I will still need to make sure that those routes render the data properly but all in all the backend work here is done!

Comments

Popular posts from this blog

Earthquakes?

Creating a Back End Application

Model View Controller (Rinse and Repeat)