CPNT 262 Day 16 - Express JSON routes
Housekeeping
Trophy of the Day
-
A REST API with the endpoints:
GET /api/your-object-here
GET /api/your-object-here/:id
1. Higher order functions with array.find()
Terminology
- Higher order function
- A function that hides (abstracts) information so we can tackle a problem at a higher (more abstract level). Examples:
Array.forEach()
,Array.filter()
,Array.find()
.
Materials
- Search: "js higher order functions"
- Reference
- Gists:
Key Takeaways
Array.find()
loops through an array of objects and expects a Boolean return value:- If
true
is returned, the loop stops andArray.find()
returns the current item; - If
false
is returned, the loop continues to the next item; - If no items are found by the end of the loop,
Array.find()
returnsundefined
.
- If
- This method is considered a higher order function, meaning it hides (abstracts) information so we can tackle a problem at a higher (more abstract level). In this case,
Array.find()
hides the fact that it's looping through the array (and that it operations differently based on a boolean result).
Activity: Practice with Array.find()
- Find your array from your Gallery
.forEach()
assignment (or some other array of Objects); - Choose a property (with a string or number value) within your objects to test against;
- Using
process.argv
andArray.find()
, create a command line app that returns the item that matches theinputValue
that is passed from the command line OR print a "Please try again" statement if no items were found.
2. Express routes
Materials
- Prep
- Review:
- How to build a REST API with Node js & Express by Programming with Mosh
- Introduction to REST
- Express Routes in the ExpressJS Documentation
- Review:
- Sample Code
3. Route parameters and request.params
Materials
- Prep
- Route parameters (linked to "Route parameters" section of "Express Routes" above)
- Step 3 - Using
req.params
with Routes in How To Retrieve URL and POST Parameters with Express on Digital Ocean - Reference:
req.params
- Sample code