Index
Prerequisite
Overview
Running the Example
Navigating the Example
Example Navigation Tips
Modules
Themes
async and await
Building Your Own
Prerequisite
Install Mongo database. See Wexler book 13.1.
You do
not
need to do any pre-setup of any databases. This can be done in the web server code.
Overview
This is a "Getting Started" example that will begin us towards an interactive web app using a database.
In this example we'll define our first database schema (data) and seed it and then show it.
"Seeding" means initializing the database with some data.
Running the Example
To run the example:
Download and unzip
this file...
Open console, navigate to unzipped project dir
Run the command "npm install"
Run command "node main"
Open your web browser to "http://localhost:3000/"
Navigating the Example
The example consists of familiar element types: (JS, CSS, HTML).
Example Navigation Tips
"models/def.js" is our database schema for a quick definition (the example calls a "def")
You will see both data and behaviors (methods) in "def.js"
"public/css" is where we code our stylesheets
"views" is our view files (EJS)
At the top of "list.ejs" you will see how we reference a stylesheet
"models/loader1.js" is where we "seed" the database
Modules
If we are going to start an example from scratch to follow an example we want to know what modules we need.
This is easy to do. We look in "package.json" under "dependencies". Thus we know we would need to do:
npm install ejs express express-ejs-layouts method-override mongoose
📋
Themes
We aim to keep the primary logic in the modeling layer.
Therefore, we will see that the middleware code is lightweight.
Think of "middleware" as between the model and view (or request).
async and await
We're going to utilize our recent study of asynchronous and synchronous to learn two advanced js keywords called
async
and await.
You will see them commented in "defs-middleware.js" and "".
Building Your Own
To build your own, the best place to start is with "models/def.js". Change that filename and source to suit your own idea -- e.g. movie.js, contact.js, recipe.js, etc as you choose.
Then modify the loader/seeder, middleware, and view for your data.
Introduction To Node.js
Quick Def Seed Show
Search Site
Home
Back To Top