Great work getting here! You're a MongoDB with Node.js hero. And Leafie can access and add to his NYC deli database. That's super exciting!
Let's take our API even further. With the basic knowledge of routes and how to connect to MongoDB in Node.js, we'll look
a bit at setting up an application following the Model-View-Controller (MVC) design pattern. Well, we don't have much a
view necessarily, but let's tackle models and controllers. Let's start a new project in a mongoose_app
directory.
Recall that starting a new Node.js project can be done with:
npm init
We can copy the dependencies from the package.json
in our original project into this one to save some time.
Another extremely common tool when working with a Node.js, Express, and MongoDB project is to use
the Mongoose ORM package.
npm install --save mongoose
Let's start with our model inside a models
subfolder. Mongoose allows us to define a schema to follow.
const mongoose = require('mongoose');
const deliSchema = mongoose.Schema({
name: String,
rest_id: Number,
street: String,
district: String,
city: String,
state: String,
postal_code: Number,
specialty: String
}, {collection: 'delis'});
module.exports = mongoose.model('Deli', deliSchema);
Mongoose ORM
Quest | Description | |
---|---|---|
Quest Zero | First quest to read. Provides some overview information and background. | |
Quest 1 | Initialize your project and install some of the required software. | |
Quest 2 | creating a quick test JavaScript. | |
Quest 3 | Crafting your own tools: Building a server process using httpd and express |
|
Quest 4 | Building Your App | |
Quest 5 | Defining the data for our application | |
Quest 6 | Enter MongoDB - Native JavaScript | |
Quest 7 | Creating and Reading data using the Native MongoDB Driver | |
> | Quest 8 | Mongoose - Object Data Modeling |
Quest 9 | Creating and Reading data using Mongoose |
Next quest: Quest 9 - Creating and Reading data using Mongoose