Node.js: Novice to Ninja is your hands-on guide to learning everything needed to build a professional web application using Node.js, the hugely popular open-source, cross-platform, back-end JavaScript runtime environment. You'll start off by getting familiar with the basics - installation and building your first apps - before moving onto more advanced concepts, including debugging, asynchronous programming, using Express.js, working with databases, deployment, and more. You'll finish by building a complete multi-player real-time quiz application.
- Install Node and build your first application
- Get started with Express.js and use it to process forms
- Master asynchronous programming in Node.js
- Work with MongoDB and MySQL databases
- Use WebSockets for real-time applications
- Understand the Node ecosystem: npm and modules
- Build a complete multiplayer quiz app
- And much more!
The book includes access to 25 high quality tutorial videos, containing 1.5 hours of content.
Author(s): Craig Buckler
Publisher: SitePoint
Year: 2022
Language: English
Commentary: True PDF
Pages: 410
Node.js: Novice to Ninja
Notice of Rights
Notice of Liability
Trademark Notice
About Craig Buckler
About SitePoint
Table of Contents
Preface
Prerequisites
Code Samples
Tips, Notes, and Warnings
Hey, You!
Ahem, Excuse Me ...
Make Sure You Always ...
Watch Out!
What is Node.js?
Skip Ahead?
JavaScript, JScript, ECMAScript, ES6, ES2015?
Why Learn Node.js?
It’s JavaScript
JavaScript Alternatives
It’s Fast
It’s Real-time
It’s Lightweight
It’s Modular
It’s Extendible
It’s Open Source
It’s Everywhere
What About Deno?
Summary
Quiz
Install Node.js
Node Version Manager
Choosing a Node.js Version
How to Install Node.js on Linux (or Windows WSL2)
Configuring npm Global Permissions on Linux
How to Install Node.js on macOS
How to Install Node.js on Windows
How to Install Node.js on Other Devices
Run JavaScript Commands in the Node.js REPL
Summary
Quiz
Your First Node.js Application
Your First Console App
#!/What?
Your First Web Server App
Port 3000?
Restarting Node.js Applications with Nodemon
Executing Scripts from Windows Powershell
Web Application Considerations
Complexity Ahead
Write Stateless Applications
Summary
Quiz
How to Debug Node.js Scripts
Skip Ahead?
What is Debugging?
How to Avoid Bugs
Use a Good Code Editor
Use a Code Linter
Use Source Control
Adopt an Issue-tracking System
Use Test-driven Development
Node.js Debugging Environment Variables
Node.js Debugging Command-line Options
Console Debugging
Never Use console.log()?!
Node.js util.debuglog
Debugging with Logging Modules
Node.js V8 Inspector
Debugging Node.js Apps with Chrome
Not Using Chrome?
Remote Target
Debugger Statement
Debugging Node.js Apps with VS Code
Advanced Debugging Configuration
Other Node.js Debugging Tools
Exercise: Debugging webhello.js
Summary
Debugging Terminology
Quiz
Getting Started with Express
Why use Express?
Express Version
Create a New Node.js Project
Create a New Git Repository
Semantic Versioning
Switch to ES6 Modules
Install Express
Runtime Dependencies and Development Dependencies
Create the Express Entry Script
What Is Routing?
Should You Switch to HTTPS?
Serve Static Files
Efficient Static Assets
Express Middleware Functions
Define Working Directories
Compressing HTTP Responses
Disable Express Identification
Handle 404 Not Found Errors
Add an HTML Template Engine
Template Performance
Advanced Routing
Routing Path Expressions
Routing Path Parameters
HTTP Route Methods
Creating a Route Handler
Exercises
Summary
Quiz
Processing Form Data with Express
Code Examples
Sanitize User Input
Processing HTTP GET Query Strings
Processing HTTP Post Body Data
The body-parser Module
Processing Uploaded Files
Callback Functions
Exercises
Summary
Quiz
How to Use the npm Node Package Manager
npm Alternatives
Global vs Local Packages
npm link
npm Help
npm Configuration
Project Initialization
Semantic Versioning
Project Dependencies
Development Dependencies
Searching for Packages
Development Dependency Limits?
Installing Packages
.gitignore node_modules
Shortcut Aliases
Semantic Constraints
“No-install” Execution
npx Local Execution
Listing Packages
Finding Outdated Packages
Update npm with npm
Removing Packages
Using npm Scripts
Special Scripts
Pre and Post Scripts
Life Cycle Scripts
Sophisticated Scripting
Publishing Packages
Publication Preparation
Two-factor Authentication
Publishing Tips
Exercises
Summary
Quiz
Using ES2015 and CommonJS Modules
Skip Ahead?
CommonJS
ES2015 Modules (ESM)
Importing External URLs
Comparison of CommonJS and ES2015 Modules
Importing CommonJS Modules in ES2015
Requiring ES2015 Modules in CommonJS
Using ES2015 Modules in Browsers
Summary
Quiz
Asynchronous Programming in Node.js
Single-threaded Non-blocking I/O Event-looping What?
Callbacks in Action
The Event Loop
Avoid Blocking the Event Loop
Callback Conundrums
A Function Must be 100% Synchronous or 100% Asynchronous
process.nextTick()
Callback Hell
Promises
util.promisify()
then() Functions Are Promisified
Parallel Promises
Promising Problems
async/await
Promise.all() is Still Necessary
try/catch is Ugly
Asynchronous Awaits in Synchronous Loops
Exercises
HTTP Requests
Summary
Quiz
Using Database Storage
Skip Ahead?
A Database-driven Web Application Example
Installing and Configuring Database Software
MongoDB
Start the MongoDB Application
Your Own MongoDB Installation?
MongoDB Functionality
What Is a Database Index?
Stop the MongoDB Application
MySQL
Start the MySQL Application
Your Own MySQL Installation?
MySQL Functionality
No Time?
UNHEX? INET_ANON?
Never Build SQL Strings!
Stop the MySQL Application
Sequelize ORM
Start the Sequelize ORM Application
Your Own MySQL Installation?
Sequelize ORM Functionality
How to Choose the Right Database
Native vs ORM Drivers
Exercises
Summary
Quiz
Using WebSockets
Skip Ahead?
What Are WebSockets?
Example WebSocket Chat Application
WebSocket Walkthrough
Advanced WebSockets Considerations
Multiple WebSocket Servers
What is Pub–sub?
Exercise
Summary
Quiz
Useful Node.js APIs
Module node: URL Imports
Process
exit Events
OS
Util
URL
File System
fs and path
Events
Events in Client-side JavaScript
Streams
Worker Threads
Asynchronous Calculations
Workers and Event Loops
Child Processes
A Real-world Example
Clusters
Write Stateless Applications
Exercises
Summary
Quiz
Example Real-time Multiplayer Quiz: Overview
Source Code
Quizzing Quick Start
What is Docker?
Summary
Example Real-time Multiplayer Quiz: Architecture
Why Develop Using Multiple Servers?
1. One PostgreSQL Database Server
2. Two Express HTTP Web Servers
3. Three WebSocket Servers
4. One Traefic Load Balancer
5. Adminer Database Client
Docker Development Environment
Docker Production Environment
Is Docker Compose Suited to Production?
Summary
Example Real-time Multiplayer Quiz: Express Code
Database Library
Question Database Initialization
Initializing Data on Application Start?
Why Does the Number of Imported Questions Vary?
Native Node.js Fetch()
Sequential Database INSERTs
Starting a New Game
Joining a Game
Quiz Page
Summary
Example Real-time Multiplayer Quiz: WebSocket Code
Initiating a WebSocket Connection
WebSocket Message Format
PostgreSQL Pub–sub
Game Logic
Joining a Game
Why Run a create() Method?
Starting a Game
Answering a Question
Leaving a Game
Exercises
Summary
Node.js Tools and Resources
Perfect Package Pursuit
Development Tools
Testing
Logging
Full-stack Frameworks
Server-side Frameworks
Node.js in Client-side Frameworks
Web Publishing, Content Management Systems, and Blogging
Headless Content Management Systems
Static Site Generators
Database Drivers
Templating
Command Line
File System
Network
WebSockets
Images
Email
Security and Authentication
Summary
Node.js Application Deployment
Pages vs Applications
Node.js Application Preparation
Dedicated Server Hosting
sudo-not
HTTP and HTTPS Considerations
Process Management
PM2 Port Clashes
Use a Reverse Proxy
Static Site Hosting (Jamstack)
Build PHP-powered Static Sites with Node.js
Serverless/Lambda Functions
AWS Everywhere
Container Hosting
Summary
Epilogue
Is Node.js for You?
Is Deno Better?
Thank You for Reading!
Quiz Answers
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12