Day 13: Models, Routes and Custom Modules
Reminders for Tony
- Put on mic
- Open Zoom chat
- Record! Anyone else?
Topics Covered
- EJS quickie
- Model fixtures with
require()
andmodule.exports
- Dynamic endpoint handlers with
request.params
Goal for the day
We’ll start the day with a long list of animal endpoint handlers. By the end of the day, we should have all animal pages served with one endpoint (GET /:id
).
Topic 1: EJS quickie
We will discuss the EJS alternative to Pug. While Pug has some great features, it can be difficult to create page partials, so we will quickly walk through the example from yesterday using EJS.
Activity objectives
- Choose an engine: Pug/EJS
- Convert your Assignment 2 html files to view files.
By the end of this activity, you should have no static html files in your public
directory. They should be moved to your views
directory.
Mid-day Huddle
- who needs help?
- any pivots?
- any show-and-tell?
[lunch]
Topic 2: Model fixtures with require()
and module.exports
We will discuss the model in deeper detail.
- Model-View-Controller
- Function scope example
- Requiring modules in Node.js: Everything you need to know
Activity objectives
Assignment 1 and 2 linked your Destinations to random websites. Try creating a destinations.js
module that you can use to import your destinations model into your app.
Topic 3: Dynamic endpoint handlers with request.params
See:
- Express Tutorial Part 4: Routes and controllers
- Understanding the req Object in Express.js
- request.params
Activity objectives
- Using today’s finished code, refactor the
GET /:id
endpoint so that it uses the animaltitle
instead.
Summary
- any trophies?
- prep for tomorrow?
- applause