
Introduction to Node.js, v3
Learning Paths:
Table of Contents
Introduction
Node Basics
History of Node.js
Scott summarizes Node.js as a JavaScript runtime used outside of web browsers. Created by Ryan Dahl in 2009, it overcame web browser limitations and server-side scripting inefficiencies.Non-Blocking I/O
Scott explains the non-blocking I/O model employed by Node.js to handle concurrent connections in high-performance network applications. Key features, such as event loops, callbacks, non-blocking APIs, and its single-threaded nature, contribute to its efficiency.Hello World
Scott walks through the setup of Node.js and a version manager called nvm. The segment includes creating a simple Node.js program that prints to the terminal.Browser vs Node.js
Scott explains the distinctions between how JavaScript is handled in the browser and Node.js, offering an overview of the Node.js REPL. The segment covers differences in the global object, modules, DOM, and console.
Creating a CLI
Process & Environment
Scott explains the global process object, highlighting commonly used properties and methods attached to it. The segment also covers using process.env to access environment variables.Custom CLI Setup
Scott explains command-line interfaces, demonstrates creating a Node.js project using npm, and covers the contents of the generated package.json file. The segment also includes linking to the file and defining the script's environment with a hashbang.Processing CLI Arguments
Scott walks through the process of building out the logic for a custom note-taking CLI, enabling it to accept input from the terminal as a new note.
Modules
Modules Overview
Scott discusses Node.js modules, which are self-contained pieces of code designed to perform specific tasks. Node.js has three types of modules: internal, user-created, and third-party modules.Importing & Exporting Modules
Scott demonstrates how to import both internal and external modules in Node.js. The differences between using `require` and `import` for including external modules are also explained in this segment.Thinking in Modules
Scott discusses things to keep in mind when developing modules for Node.js apps. This segment covers topics such as module contents, export and import patterns, and index.js patterns.Internal & 3rd-Party Modules
Scott discusses built-in modules that are available within the Node.js environment. Installing, updating, and removing third-party modules using npm is also covered in this segment.Using the yargs Module
Scott demonstrates the installation and usage of the third-party yargs module to implement a help command for the custom note-taking CLI.Notes App Commands
Scott sets up the foundation for CLI commands such as new, all, find, remove, web, and clean.
File I/O
Async Code
Scott explains the benefits of asynchronous code, which allows other operations to occur while waiting for a task to complete. A demonstration of how to avoid nested callbacks using promises is also covered in this segment.FS Module
Scott demonstrates how to use the built-in FS module, which provides an API for interacting with the file system. Student questions regarding debugging errors involving nesting callbacks are also covered in this segment.Using a File as a DB
Scott demonstrates how to use the FS module and a JSON file as a database to store created notes. The foundation for utility functions to read the db.json file, save a JavaScript object, and add a new note are also covered in this segment.CRUD Methods: Create
Scott explains CRUD operations, which involve create, read, update, and delete actions on the notes in the database. Abstracting the previously created insert utility function for creating new notes and a student's question about destructuring are also covered in this segment.CRUD Methods: Read & Delete
Scott develops a function to fetch all notes from the database and filters them based on the provided filter string. A demonstration of the process of removing notes with specific IDs and clearing all notes from the database in this segment is also provided in this segment.Using the CRUD Methods
Scott updates the commands to utilize the previously created CRUD methods, allowing for better functionality and interaction with the notes in the database.
Testing
Types of Tests
Scott discusses the different types of testing in a Node.js application, which include unit testing, integration testing, end-to-end testing, and API testing.Unit Testing with Jest
Scott demonstrates writing and running tests using the Jest testing framework. Jest automatically scans for test files in the tests directory and executes them.Testing with Mocks
Scott discusses testing with mock functions and data to perform tests without modifying the actual data in the database. Mock functions, also referred to as "spies," observe the behavior of indirectly called functions, allowing for thorough testing of function behavior and output.Additional Test Examples
Scott demonstrates additional examples of tests for different functionalities and use cases.
Servers
Creating a Basic Server
Scott discusses the concept of a Node.js server, which listens for incoming network requests and responds by executing code or providing data. A walk through the process of creating a basic Node.js server is also covered this segment.Interpolating Data & Formatting Notes
Scott showcases how to interpolate the notes data, inserting or embedding values into a string or template to dynamically create a new string. How to define the format of the returned notes is also covered in this segment.Sending Notes to the Client
Scott illustrates the utilization of the createServer function to take the array of notes and generate a Node.js HTTP server that serves the formatted HTML to the client.