
Digging Into Node.js
Learning Paths:
Table of Contents
Introduction
Introduction
Kyle introduces the course on Node.js by describing why Node is significant to JavaScript and the tech community, and gives an anecdote about handling interactions between the front end and the back end.The Middle End
Kyle 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 Perspective
Kyle discusses what Node was intended to be used for and what it does well, while highlighting its asynchronous event loop.Console Log & Process stdout
Kyle 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 stderr
Kyle 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 Script
Kyle 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 Arguments
Kyle 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 Handling
Kyle 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 Modules
Kyle 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 readFile
Kyle discusses asynchronicity in Node, then modifies the call to read the file to make the call asynchronously with readFile.Processing File Contents
Kyle 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 stdin
Kyle 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 Variables
Kyle 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.
Streams
File Handling with Streams
Kyle 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 Stream
Kyle 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 File
Kyle 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 zlib
Kyle 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 zlib
Kyle 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 Stream
Kyle 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 & Timeouts
Kyle 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&A
Kyle 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
Database & Exercise Setup
Kyle walks through installing and setting up a database with SQLite.Read & Write from Database
Kyle 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 Exercise
Kyle instructs students to write an asynchronous function to insert an item into the database.Insert into Database Solution
Kyle live codes the solution to the exercise.Get Database Records
Kyle writes a SQL statement to read from the database to view the results of inserting records during the previous lesson.
Web Servers
Creating a Web Server
Kyle 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 Files
Kyle 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 Endpoint
Kyle 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 Exercise
Kyle 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 Solution
Kyle live codes the solution to the exercise.Express JS Routing
Kyle 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 Express
Kyle 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 Middleware
Kyle 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 Process
Child Processes
Kyle 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 Codes
Kyle 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 Exercise
Kyle 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 Solution
Kyle live codes the solution to the exercise.Child Processes Q&A
Kyle 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.