Turn a file into a module: assign a value to using module.exports.
Load a module using require().
Demonstrate a module’s protected variable environment.
Load an array of objects using a custom module
Terminology
Module
A reusable block of code whose existence does not accidentally impact other code (Javascript didn’t have this before).
CommonJS Module
An agreed upon standard for how code modules should be structured. Because modules are a relatively new feature of Javascript, there are competing standards: ES Modules are used in the browser but CommonJS Modules are most common in Node.js (which supports both standards).
You can assign any value to module.exports to expose it to the outer environment.
require() returns the value that is assigned to a module’s module.exports. All other variables will be private to the module.
3. JSON routes
Learning Objectives
Define route parameter.
Create a JSON endpoint using app.get() and custom modules.
Return an array of objects to GET /api/v0/definitions.
Review:
Requesting data using fetch().
Bonus: Demonstrate a route parameter using app.get() and :slug.
Return a single of object to GET /api/v0/definitions/:slug.
Terminology
Route parameter
A named URL segment (i.e. between or after a /) that is used to capture values based on their position in a URL. Example: in /api/image/:id, the route parameter is :id.