Dev dependencies (nodemon) and express.Router()
Homework
- Review
nodemon
- Reference: nodemon
- Watch: REST API with Node js & Express @14:57 - 16:29
- Read: Downloading and installing packages globally
dotenv
- Reference: dotenv
express.Router
- Read: ExpressJS - Routing on Tutorials Point
- Reference: Express Routes -
express.Router
Morning reflection
Housekeeping
1. nodemon
and dotenv
Learning Objectives
- Understand the purpose of development dependencies
- Install
nodemon
as a development dependency. - Add a
dev: "nodemon server.js"
script topackage.json
. - Start a server using
nodemon
.
- Install
- Understand the purpose of environment variables.
- Install
dotenv
as a dependency. - Assign a
PORT
environment variable. - Load a
.env
file into a Node server.
- Install
Terminology
- Development Dependency
- Packages that are only needed for local development and testing.
- Environment Variable
- Configuration variables that are set to different values based on the environment you run your app in.
Materials
Key Takeaways
-
Install development dependencies with the
--save-dev
or-D
flags:$ npm install nodemon --save-dev
Or
$ npm install nodemon -D
- Development dependencies are installed when you run
npm install
. To prevent this you instead runnpm install --production
. - Installing a dependency globally is not recommended for production dependencies (you’re locked to one version of
express
, for example) but global dev dependencies are usually fine. - Permissions issues sometimes happen with global packages depending on the system.
2. express.Router()
Learning Objectives
- Understand the purpose of the
express.Router
class. - Create router module with
express.Router
. - Export a route using
modules.exports
. - Load a route using
app.use()
.
Materials
Key Takeaways
- Router modules still need to load
express
withrequire()
. - Router methods (i.e.
router.get()
) behave largely the same as the parent express methods (i.e.app.get()
). -
Route modules append their endpoint paths to the path specified in the
app.use()
that loaded it. Together, these paths are joined to create the complete route path (i.e./api/v0/definitions
).In
server.js
:app.use('/api/v0', v0);
In
/routes/api/v0.js
:router.get('/definitions', callback);
3. Assignment 5, Final project and group assignments
- DRAFT: Assignment 5
- DRAFT: Final Project
- You will be assigned to your Final Project groups during lab time.
Open lab-time
- Group hangs
- Team names!
- Mentor orientation
Dailies
- Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.