Standard Library
Node is not just a server-side JavaScript runtime, it also includes a robust collection of libraries or “core modules” for managing the filesystem, streaming data, events and more! We will get some hands-on experience with a broad array of commonly-used libraries, so you know what is available and when to reach for it.
-
Standard LibraryBuffers, Files and Paths
Two of the most widely-used Node.js APIs are
fs
, used to interact with the filesystem, andpath
, used to construct proper resource paths that work on all operating systems. We’ll look at basic tasks like reading and managing files; how the “current directory” is affected by the location of our source code; andfs-extra
, a popular open source library that provides a range of commonly-needed filesystem tools beyond those that ship with Node. -
Standard LibraryEXERCISE: Directory-to-JSON and back again
You have been presented with a hierarchy of text files and folders. Build a
filesToJson
function that, given the path to a folder, generates a single JSON object representing the contents (and placement) of the files therein. Then, build ajsonToFiles
function that takes your JSON object along with a folder path, and write the contents to disk as the appropriate hierarchy. -
Standard LibraryEvents
A large portion of the Node api is built around the idea of “event emitters” to allow the registration of “listeners” (functions), which are invoked whenever an event is fired. We’ll learn about creating and consuming our own
EventEmitter
, and learn when it is appropriate to respond to events synchronously or asynchronously. -
Standard LibraryEXERCISE: Operating an Oven
We’ll write some code to control an oven so that we can bake some Node-powered cookies! Your job is to build an
EventEmitter
that allows the operator of the oven to do their job in response to thepowerOn
,powerOff
,doorOpen
,doorClose
, andtemperatureReached
events. -
Standard LibraryStreams
Streams are a particular type of collection, where data may not be available all at once. When working with a stream of data, we can start immediately processing whatever we have so far, without requiring that “everything” is placed in memory. While it is somewhat uncommon for developers to create new types of streams, you’ll end up working with them extensively as we get deeper into Node.
-
Standard LibraryEXERCISE: Oven Thermocouple
You’ll be provided with some code that represents a kitchen oven. It is set to a “desired temperature”, the heater turns on, and a temperature sensor lets us know when the oven is ready. However, we have a problem… the temperature sensor is cheap and produces a garbage reading sometimes.
Write a program to consume the raw data stream from an oven’s temperature sensor to control the heating system, in order to keep the oven temperature pretty close to what the user set it at. You’ll need to use a
Transform
stream, and expose a stream for the oven’s heater to “listen” to. -
Standard LibraryWrap Up
We'll recap what we have learned so far, and outline the topics to be covered tomorrow. Homework may be assigned.