CPNT 262 Day 12 - Introduction to Node builtin modules
Housekeeping
Trophies of the Day
- A command line app!
1. Node Global Objects
Materials
- Node global objects
- Reference:
Array.slice()
- Gist: Default Node objects and properties
Activities
- Adding command line arguments with
process.argv
- Tip Calculator from the command line with
process.argv
- Greeting App from the command line with
process.argv
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
- file paths
- 'path builtin module'
- Gists:
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 ofprocess.argv
are absolute paths or not.
3. fs
module
Materials
Key Takeaways
- Read files asynchronously whenever possible.
- The "dot-slash" characters
./
at the beginning of a path mean it's location is relative to the current script. fs.readFile()
andfs.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
- Javascript basics
- Strings
- Arrays
- Javascript Objects: Getting started
- Functions
- Loops
Prep
Node basics
- Video Node.js Tutorial for Beginners: Learn Node in 1 Hour by Programming with Mosh