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

Key Takeaways

  • Array.find() loops through an array of objects and expects a Boolean return value:
    • If true is returned, the loop stops and Array.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() returns undefined.
  • 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()

  1. Find your array from your Gallery .forEach() assignment (or some other array of Objects);
  2. Choose a property (with a string or number value) within your objects to test against;
  3. Using process.argv and Array.find(), create a command line app that returns the item that matches the inputValue that is passed from the command line OR print a "Please try again" statement if no items were found.

2. Express routes

Materials


3. Route parameters and request.params

Materials

Activity