
Introduction to Node.js, v2
Table of Contents
Introduction
Introduction
Scott Moss begins the course by sharing the course website which contains the notes, resources, and code snippets.Installing Node.js
Scott describes how Node.js is great for a wide range of use cases like API development, CLI's, and automations. Node.js can be installed from a downloadable installer or by using the Node Version Manager which can install and switch between specific versions of Node.js.Executing Node
Scott demonstrates how to execute JavaScript code in the Node REPL. A REPL is like the console in a browser because it executes code and displays the result. Since Node.js is an OS-level runtime, it will not understand any browser-specific JavaScript API's.Node.js Intro Q&A
Scott answers questions about who decides what's included in Node.js, what has changed in version 2 of this course, the differences between Deno and Node.js, and how to test browser-based JavaScript in Node.js.
Basic Components
Globals
Scott walks through the global properties available in the Node.js environment. The most common global properties are __dirname, __filename, and process. The exports, module, and require properties are used for creating and exposing modules across the app.Modules
Scott describes a module as a reusable chunk of code that has its own context. By default, Node.js uses CommonJS modules. Newer versions of node support ES6 modules. Questions about migrating a project to ES6 modules and module conflicts when mixing CommonJS with ES6 modules are also covered in this segment.File System
Scott demonstrates how the fs module is used to read and write from the file system. The fs module contains promise-based methods that allow the use of async/await. This creates more linear file-handling code and avoids nesting application code in callbacks.Error Handling
Scott demonstrates how to handle asynchronous code errors with callbacks, promises, and async/await. When using async/await, a try/catch block allows the developer to decide how to handle an error. If error handling is not possible because of a module's implementation, adding an uncaughtException event to the process object can be used to respond to any uncaught error.Errors and Async Await Q&A
Scott answer questions about using the global process property, error objects vs. exceptions, and the behavior of async/await.
Packages
Creating Local Packages & npm
Scott explains how to create a package that could be consumed by any Node.js application or distributed using the npm package manager. Packages have a package.json configuration file that contains all the information about the package including the version, dependencies, and scripts for building or running the application.Finding & Installing Packages
Scott demonstrates how to search for packages in the npm registry. Researching a package's dependencies, Github repository, and version history will help determine if the package is well supported or active. The command 'npm install' is used to install a remote package into a project. The remote package will be added to the list of dependencies in the package.json file.Using npm Packages
Scott demonstrates how to consume packages installed from the npm registry inside a module. Global package installation and the difference between npm and npx are also covered in this segment.Running npm Scripts
Scott explains how to add custom scripts to the package.json file. The scripts could be used to build the package, start a development environment or create a local web server.
CLI
Setup a CLI Script with Node.js
Scott creates a command line interface (CLI) using Node.js. By including the installation directory at the top of the module and specifying the command on the bin object in the package.json file, the Node.js package can be installed globally using npm and executed from the command line like other CLI tools.Building a Reddit CLI
Scott creates a Reddit CLI tool that fetches a random post from the Reddit API and opens the post in a web browser. If a '--print' flag is passed to the CLI, the title and description will be printed to the command line and the CLI will not open the browser.
Servers
Creating a Low-Level Server
Scott uses the http module to create a simple web server. The createServer method is passed a function to handle each request and response for the server. The request object contains information about the request along with any data sent to the server. The response object returns a status code and any results after processing the request.Testing an API with HTTPie
Scott uses the HTTPie command line tool to interact with the Node.js server. HTTPie allows requests to be sent to a server endpoint and displays the response. Questions about other CLI frameworks are also covered in this segment.Create a Server API with Express
Scott uses Express to create a server API. Express is a framework for building servers. It uses the body-parser middleware to parse incoming requests. Endpoints or routes are defined using the get and post methods. Express adds helper methods to the response object to return the result from a request.Dynamic Routes in Express
Scott implements dynamic routing in Express to access specific TODO items. The deprecation of the body-parser module is also covered in this segment.
Testing
Vanilla Unit Tests
Scott explains that unit tests will test little chunks code in isolation to ensure they behave has intended. Node.js ships with the assert module which provides utilities to create expectations. When those expectations aren't met, assert will throw an error.Jest
Scott installs Jest as a development dependency in the project and demonstrates why it is a better testing tool than using the built-in assert module. After the test suite is executed, the number of passing and failing tests are displayed in the command line along with a description of the tests that were run.Asynchronous Tests
Scott writes tests for the asynchronous code in the module. An async function is passed as the callback to Jest's test method. The method being tested is called and the response is compared to the expected results. Jest can also test for errors by indicating how many assertions should be met during a test. If an assertion is not reached, the test will fail.Debugging
Scott demonstrates how to debug applications using Visual Studio Code and the Chrome developer tools. Breakpoints can be added which cause the debugger to halt at a specific line and allow local/global variables and the current call stack to be analyzed.
Deployment
Deployment: Packages
Scott publishes the Reddit CLI application to the npm registry. A package is published with the command 'npm publish'. Once the package is published, it can be installed by anyone using npm.Deployment: Servers
Scott demonstrates how to deploy the server application to Heroku. When deploying to any hosted service, a 'start' script should be declared in the package.json file so the server knows which file to start. Any hard coded server settings are replaced with environment variables.