You Don't Know JS
Course Description
Node.js has taken the JavaScript world by storm, but where do you start when all you see are tens of thousands of packages/frameworks on npm? The best place to start is always at the foundation, and that's what this course focuses on. In this course, you’ll get to know the fundamental concepts in Node.js: CLI programming, file system access, asynchrony, streams, HTTP servers & routing, database persistence, and child processes.
This course and others like it are available as part of our Frontend Masters video subscription.
Preview
CloseWhat They're Saying
Great intro to Node.js by Kyle Simpson. I appreciate the points about the inception of Node.js and the rationale behind choosing JavaScript as an main language. It's important to understand which types of tasks are well-suited for Node.js.
D
d_vallbs
Learn Straight from the Experts Who Shape the Modern Web
Your Path to Senior Developer and Beyond
- 200+ In-depth courses
- 18 Learning Paths
- Industry Leading Experts
- Live Interactive Workshops
Table of Contents
Introduction
Section Duration: 35 minutes
- 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.
- 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.
- Kyle discusses what Node was intended to be used for and what it does well, while highlighting its asynchronous event loop.
- 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".
- 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
Section Duration: 44 minutes
- 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.
- 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.
- 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.
- 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.
- Kyle discusses asynchronicity in Node, then modifies the call to read the file to make the call asynchronously with readFile.
- 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.
- 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.
- 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
Section Duration: 59 minutes
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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
Section Duration: 25 minutes
- Kyle walks through installing and setting up a database with SQLite.
- 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.
- Kyle instructs students to write an asynchronous function to insert an item into the database.
- Kyle live codes the solution to the exercise.
- Kyle writes a SQL statement to read from the database to view the results of inserting records during the previous lesson.
Web Servers
Section Duration: 44 minutes
- 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.
- 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.
- 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.
- 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.
- Kyle live codes the solution to the exercise.
- 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.
- Kyle configures Express to handle requests for static files with the app.use method, adding a custom header and utilizing the Express static server.
- 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
Section Duration: 24 minutes
- 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.
- 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.
- 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.
- Kyle live codes the solution to the exercise.
- 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.
Debugging
Section Duration: 5 minutes
- Kyle demonstrates how to use Chrome's developer tools to debug Node programs as an alternative to logging to the console.
Wrapping Up
Section Duration:
- Kyle concludes the course by urging students to continue learning about streams and working on building projects with Node.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops