Can you modify this CRUD app to use MongoDB?
Install application packages/dependencies:
bower install
npm install
Run the server:
nodemon index.js
Our foods data is currently hardcoded in index.js
and lives in active memory. When the server restarts, we lose our data. Your goal is integrate MongoDB into your routes so that you can permenantely save/persist data across sessions.
By the end of this process we should have the following application directory (note the models
folder):
models/
index.js
food.js
public/
css/
main.css
js/
app.js
views/
index.html
.gitignore
bower.json
index.js
package.json
README.md
Install mongoose:
npm install --save mongoose
Next we want to create our models directory (this is where our database magic will happen):
mkdir models
touch models/food.js
Let's create Food
model. A Food
has a few different characteristics: name
, and yumminess
.
To create a Food
model we have to use a Schema:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var FoodSchema = new Schema({
name: String,
yuminess: String
});
Here is a link to all of the different datatypes we can use in a Schema
Finally, at the bottom of food.js
we need to create the Food
model and export it (that way we can use it in other parts of our app):
var Food = mongoose.model('Food', FoodSchema);
module.exports = Food;
Next, let's wire it all up:
touch models/index.js
Inside of models/index.js
we need to create and connect to our mongoose database (this is where the magic happens):
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/bite_me_app");
module.exports.Food = require("./food");
Now that we've done all the hard work, all that's left is to "require" our models.
If you open the node REPL by typing node
in the terminal, you can do the following:
var db = require('./models');
db.Food.create({name: "foo", tastiness: "very"}, function (err, food) {
if (err) { return console.log(err); };
console.log(food);
});
Now you should be able to type the following, and see our new food item:
db.Food.find({}, function(err, foods){
if (err) { return console.log("BAD THING!"); }
console.log(foods)
})
Now let's do it in the express app!
Add this line near the top of index.js
:
var db = require('./models');
The time has come for us to swap out all our hardcoded data for live data from our database. This will require us to methodically alter each route to accomodate the new data store. Where should we start!?
Hint: Here's what our index route might look like:
app.get("/allthefoods", function(req, res){
db.Food.find({}, function(err, foods){
if (err) {
console.log("BAD THING!");
return res.sendStatus(400);
}
res.send(foods);
})
})