CPNT 262 Day 12 - Introduction to Node builtin modules

Housekeeping

Trophies of the Day

  • A command line app!

1. Node Global Objects

Materials

Activities


2. Builtin modules

  • path module

Terminology

Node Builtin Module
Modules that are included with the Node runtime environment. These modules need to be imported into your script but do not need to be installed with npm.

Materials

Key Takeaways

  • Although builtin modules come pre-installed with Node, they still need to be imported into your script with require().

Activities

  • Using the path builtin module and the __filename constant, create a node app that prints the file name (without directory) and extension of the current script.
  • Using path.isAbsolute, determine if the first two arguments of process.argv are absolute paths or not.

3. fs module

Materials

Key Takeaways

  1. Read files asynchronously whenever possible.
  2. The "dot-slash" characters ./ at the beginning of a path mean it's location is relative to the current script.
  3. fs.readFile() and fs.readdir() use "error-first callbacks" where the first argument passed to a callback is the error, if there was one. This is a common pattern in Node.

4. Lab Time

Looks like you need inspiration for Assignment 4!

  • Can any of the following sample code be refactored to take arguments from the command line?

Miscellaneous Node-friendly Gists

Prep

Node basics

Reference