Digging Into Node.js
Table of Contents
The Middle EndKyle uses a story from past career experience to introduce the idea of the middle end, and explains how the concept can be used by front end developers to treat the back end of the site agnostically from the front end.
Node PerspectiveKyle discusses what Node was intended to be used for and what it does well, while highlighting its asynchronous event loop.
Console Log & Process stdoutKyle writes a Node program. First, questions are asked about how to get information into and out of the program. Then, logging to the console is compared against writing to standard output using "process".
Console Error & Process stderrKyle writes a message to console error and console log, which use two different streams of output. Then, the information from the standard out stream is redirected to demonstrate different facets of write streams.
Command Line Scripts
Setting Up a Command Line ScriptKyle writes a basic command line script, using a "hashbang" symbol to control how the program is interpreted. Then, file permissions are changed to make the first exercise file an executable using the "chmod" command, and a function to print static help text is created.
Command Line ArgumentsKyle uses process.args to receive command line arguments, gets an array of the arguments passed in, and then demonstrates how to utilize the minimist package to place the arguments into an object with optional configuration.
Argument HandlingKyle writes code to handle the arguments in the generated object from minimist, output a help message if the help argument is included, make sure sufficient arguments are added, and print an error if input requirements are unmet.
Reading Files with Path & FS ModulesKyle demonstrates how to use path.resolve to get a file's relative path, __dirname to get the current directory of a file, and how to print the contents of the file to the screen using the readFileSync within the "fs" module. Considerations about how the data is being passed are discussed.
Asynchronous readFileKyle discusses asynchronicity in Node, then modifies the call to read the file to make the call asynchronously with readFile.
Processing File ContentsKyle walks through processing a file's contents by converting a binary buffer into a string, making the characters uppercase, and writing the contents to standard output. The efficiency of the operation is then considered.
Processing Input from stdinKyle modifies the program so the contents to be processed can be optionally pulled from the standard input stream, and introduces the util and get-stdin packages.
Environment VariablesKyle introduces environment variables for the shell with a few of their use cases, and configures the program to receive and utilize environment variables. The path.join method is introduced.
File Handling with StreamsKyle explains what it means for a stream to be readable versus writable, what simplex and duplex streams are, how pipe and chaining pipe calls work, and then modifies the program to utilize a stream from the file instead of reading the file contents into a buffer.
Transform StreamKyle sets up a transform stream, which enables processing data item by item in the middle of a stream pipe. The efficiency of the new processing method is covered.
Outputting a Stream to a FileKyle makes changes to the program such that if printing to standard output isn't specified, the output will be a text file, enabling a choice between the two streams. The help message is modified to reflect this change.
gzip Compress with zlibKyle pulls in Node's built in zlib module, which handles gzip compression, and writes code to create a streaming gzip transform stream in order to compress incoming data and then send it into the output stream.
gzip Uncompress with zlibKyle continues adding gzip functionality to the program, enabling uncompression with the zlib Node module. Then, results are displayed for executing the program with different command line arguments.
Determining End of StreamKyle demonstrates how to determine when a stream has ended by writing a function to detect when a stream's end event is fired. Then, the information is used by wiring up the processing function to be asynchronous and wait until the stream ends before completing.
Asynchronous Cancellation & TimeoutsKyle pulls in a module to enable stopping asynchronous work with cancellation tokens, utilizing generator functions, the unpipe method, and finally the destroy method to end processing.
Asynchronous Q&AKyle fields questions about awaiting multiple streams, how to use Promise.all, choosing to use a duplex stream over a simplex stream, REST APIs and streams, and why streams break data into chunks.
Database & Exercise SetupKyle walks through installing and setting up a database with SQLite.
Read & Write from DatabaseKyle explains the goal of the program, which is to use SQL commands in Node to insert and persist data in the SQLite database set up in the previous lesson.
Insert into Database ExerciseKyle instructs students to write an asynchronous function to insert an item into the database.
Insert into Database SolutionKyle live codes the solution to the exercise.
Get Database RecordsKyle writes a SQL statement to read from the database to view the results of inserting records during the previous lesson.
Creating a Web ServerKyle demonstrates how to create a web server, writes a function to handle requests to the server, and then writes code that handles whether to display a message on the page or return a default 404 page.
Routing & Serving Static FilesKyle configures the server to only allow access to the files under the web root and adds a file server that uses regular expressions to match paths of incoming requests to the server.
Serving API EndpointKyle creates functionality on the server to respond to API requests at a given endpoint by getting the records from the database, stringifying the JSON, and then writing the response to contain the stringified JSON.
Fetch ExerciseKyle instructs students to write a getRecords function that uses fetch to get the JSON data from the API endpoint and then passes the data to a prewritten function to print the formatted results.
Fetch SolutionKyle live codes the solution to the exercise.
Express JS RoutingKyle modifies the server to use Express instead of manual routing, instantiates the Express app, and configures the routes Express will handle by declaratively stating the route inside of the app.get method.
Serving Static Files with ExpressKyle configures Express to handle requests for static files with the app.use method, adding a custom header and utilizing the Express static server.
Custom Routing MiddlewareKyle creates custom middleware to handle URL rewriting and explains what Express does when a request arrives. Then, Kyle fields questions about the usage of Express across the industry.
Child ProcessesKyle sets up the functionality of creating child processes from a main process in Node, and demonstrates how to listen for the child process to exit using the object received from calling spawn to create the child.
Exit CodesKyle explains what exit codes are used for, makes a fetch request against the server, and demonstrates how to test whether the exit code is communicating success or failure from the command line.
Child Processes ExerciseKyle instructs students to run a child process, check to see if the process was successful, wait a short amount of time, and then initiate a child process again, running continuously until explicitly commanded to stop.
Child Processes SolutionKyle live codes the solution to the exercise.
Child Processes Q&AKyle fields questions about load balancing versus load testing, testing at scale, the benefits of creating child processes, what is occurring when a program fails, and alternatives to child processes in Node.
Debugging Node.jsKyle demonstrates how to use Chrome's developer tools to debug Node programs as an alternative to logging to the console.