Node.js: Novice to Ninja

Book description

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.

Publisher resources

View/Submit Errata

Table of contents

  1. Node.js: Novice to Ninja
  2. Notice of Rights
  3. Notice of Liability
  4. Trademark Notice
  5. About SitePoint
  6. About the Author
  7. Preface
    1. Prerequisites
    2. Conventions Used
      1. Code Samples
      2. Tips, Notes, and Warnings
    3. Where to Find Help
      1. The SitePoint Forums
      2. The Code Archive
    4. Your Feedback
    5. Let’s Go
  8. Chapter 1: What is Node.js
    1. Skip Ahead?
    2. JavaScript, JScript, ECMAScript, ES6, ES2015?
    3. Why Learn Node.js?
      1. It’s JavaScript
        1. JavaScript Alternatives
      2. It’s Fast
      3. It’s Real-time
      4. It’s Lightweight
      5. It’s Modular
      6. It’s Extendible
      7. It’s Open Source
      8. It’s Everywhere
        1. What About Deno?
    4. Summary
    5. Quiz
  9. Chapter 2: Install Node.js
    1. Node Version Manager
    2. Choosing a Node.js Version
    3. How to Install Node.js on Linux (or Windows WSL2)
      1. Configuring npm Global Permissions on Linux
    4. How to Install Node.js on macOS
    5. How to Install Node.js on Windows
    6. How to Install Node.js on Other Devices
    7. Run JavaScript Commands in the Node.js REPL
    8. Summary
    9. Quiz
  10. Chapter 3: Your First Node.js Application
    1. Your First Console App
      1. #!/What?
    2. Your First Web Server App
      1. Port 3000?
    3. Restarting Node.js Applications with Nodemon
      1. Executing Scripts from Windows Powershell
    4. Web Application Considerations
      1. Complexity Ahead
      2. Write Stateless Applications
    5. Summary
    6. Quiz
  11. Chapter 4: How to Debug Node.js Scripts
    1. Skip Ahead?
    2. What is Debugging?
    3. How to Avoid Bugs
      1. Use a Good Code Editor
      2. Use a Code Linter
      3. Use Source Control
      4. Adopt an Issue-tracking System
      5. Use Test-driven Development
    4. Node.js Debugging Environment Variables
    5. Node.js Debugging Command-line Options
    6. Console Debugging
      1. Never Use console.log()?!
    7. Node.js util.debuglog
    8. Debugging with Logging Modules
    9. Node.js V8 Inspector
    10. Debugging Node.js Apps with Chrome
      1. Not Using Chrome?
      2. Remote Target
      3. Debugger Statement
    11. Debugging Node.js Apps with VS Code
      1. Advanced Debugging Configuration
    12. Other Node.js Debugging Tools
    13. Exercise: Debugging webhello.js
    14. Summary
      1. Debugging Terminology
    15. Quiz
  12. Chapter 5: Getting Started with Express
    1. Why use Express?
      1. Express Version
    2. Create a New Node.js Project
      1. Create a New Git Repository
      2. Semantic Versioning
    3. Switch to ES6 Modules
    4. Install Express
      1. Runtime Dependencies and Development Dependencies
    5. Create the Express Entry Script
      1. What Is Routing?
    6. Should You Switch to HTTPS?
    7. Serve Static Files
      1. Efficient Static Assets
    8. Express Middleware Functions
    9. Define Working Directories
    10. Compressing HTTP Responses
    11. Disable Express Identification
    12. Handle 404 Not Found Errors
    13. Add an HTML Template Engine
      1. Template Performance
    14. Advanced Routing
      1. Routing Path Expressions
      2. Routing Path Parameters
      3. HTTP Route Methods
      4. Creating a Route Handler
    15. Exercises
    16. Summary
    17. Quiz
  13. Chapter 6: Processing Form Data with Express
    1. Code Examples
    2. Sanitize User Input
    3. Processing HTTP GET Query Strings
    4. Processing HTTP Post Body Data
      1. The body-parser Module
    5. Processing Uploaded Files
      1. Callback Functions
    6. Exercises
    7. Summary
    8. Quiz
  14. Chapter 7: How to Use the npm Node Package Manager
    1. npm Alternatives
    2. Global vs Local Packages
      1. npm link
    3. npm Help
    4. npm Configuration
    5. Project Initialization
      1. Semantic Versioning
      2. Project Dependencies
      3. Development Dependencies
    6. Searching for Packages
      1. Development Dependency Limits?
    7. Installing Packages
      1. .gitignore node_modules
      2. Shortcut Aliases
      3. Semantic Constraints
    8. “No-install” Execution
      1. npx Local Execution
    9. Listing Packages
    10. Finding Outdated Packages
      1. Update npm with npm
    11. Removing Packages
    12. Using npm Scripts
      1. Special Scripts
      2. Pre and Post Scripts
      3. Life Cycle Scripts
      4. Sophisticated Scripting
    13. Publishing Packages
      1. Publication Preparation
      2. Two-factor Authentication
      3. Publishing Tips
    14. Exercises
    15. Summary
    16. Quiz
  15. Chapter 8: Using ES2015 and CommonJS Modules
    1. Skip Ahead?
    2. CommonJS
    3. ES2015 Modules (ESM)
      1. Importing External URLs
    4. Comparison of CommonJS and ES2015 Modules
    5. Importing CommonJS Modules in ES2015
    6. Requiring ES2015 Modules in CommonJS
    7. Using ES2015 Modules in Browsers
    8. Summary
    9. Quiz
  16. Chapter 9: Asynchronous Programming in Node.js
    1. Single-threaded Non-blocking I/O Event-looping What?
      1. Callbacks in Action
    2. The Event Loop
      1. Avoid Blocking the Event Loop
    3. Callback Conundrums
      1. A Function Must be 100% Synchronous or 100% Asynchronous
      2. process.nextTick()
      3. Callback Hell
    4. Promises
      1. util.promisify()
      2. then() Functions Are Promisified
      3. Parallel Promises
      4. Promising Problems
    5. async/await
      1. Promise.all() is Still Necessary
      2. try/catch is Ugly
      3. Asynchronous Awaits in Synchronous Loops
    6. Exercises
      1. HTTP Requests
    7. Summary
    8. Quiz
  17. Chapter 10: Using Database Storage
    1. Skip Ahead?
    2. A Database-driven Web Application Example
      1. Installing and Configuring Database Software
    3. MongoDB
      1. Start the MongoDB Application
        1. Your Own MongoDB Installation?
      2. MongoDB Functionality
        1. What Is a Database Index?
      3. Stop the MongoDB Application
    4. MySQL
      1. Start the MySQL Application
        1. Your Own MySQL Installation?
      2. MySQL Functionality
        1. No Time?
        2. UNHEX? INET_ANON?
        3. Never Build SQL Strings!
      3. Stop the MySQL Application
    5. Sequelize ORM
      1. Start the Sequelize ORM Application
        1. Your Own MySQL Installation?
      2. Sequelize ORM Functionality
    6. How to Choose the Right Database
      1. Native vs ORM Drivers
    7. Exercises
    8. Summary
    9. Quiz
  18. Chapter 11: Using WebSockets
    1. Skip Ahead?
    2. What Are WebSockets?
    3. Example WebSocket Chat Application
    4. WebSocket Walkthrough
    5. Advanced WebSockets Considerations
      1. Multiple WebSocket Servers
        1. What is Pub–sub?
    6. Exercise
    7. Summary
    8. Quiz
  19. Chapter 12: Useful Node.js APIs
    1. Module node: URL Imports
    2. Process
      1. exit Events
    3. OS
    4. Util
    5. URL
    6. File System
      1. fs and path
    7. Events
      1. Events in Client-side JavaScript
    8. Streams
    9. Worker Threads
      1. Asynchronous Calculations
      2. Workers and Event Loops
    10. Child Processes
      1. A Real-world Example
    11. Clusters
      1. Write Stateless Applications
    12. Exercises
    13. Summary
    14. Quiz
  20. Chapter 13: Example Real-time Multiplayer Quiz: Overview
    1. Source Code
    2. Quizzing Quick Start
      1. What is Docker?
    3. Summary
  21. Chapter 14: Example Real-time Multiplayer Quiz: Architecture
    1. Why Develop Using Multiple Servers?
    2. 1. One PostgreSQL Database Server
    3. 2. Two Express HTTP Web Servers
    4. 3. Three WebSocket Servers
    5. 4. One Traefic Load Balancer
    6. 5. Adminer Database Client
    7. Docker Development Environment
    8. Docker Production Environment
      1. Is Docker Compose Suited to Production?
    9. Summary
  22. Chapter 15: Example Real-time Multiplayer Quiz: Express Code
    1. Database Library
    2. Question Database Initialization
      1. Initializing Data on Application Start?
      2. Why Does the Number of Imported Questions Vary?
      3. Native Node.js Fetch()
      4. Sequential Database INSERTs
    3. Starting a New Game
    4. Joining a Game
    5. Quiz Page
    6. Summary
  23. Chapter 16: Example Real-time Multiplayer Quiz: WebSocket Code
    1. Initiating a WebSocket Connection
      1. WebSocket Message Format
      2. PostgreSQL Pub–sub
    2. Game Logic
      1. Joining a Game
        1. Why Run a create() Method?
      2. Starting a Game
      3. Answering a Question
      4. Leaving a Game
    3. Exercises
    4. Summary
  24. Chapter 17: Node.js Tools and Resources
    1. Perfect Package Pursuit
    2. Development Tools
    3. Testing
    4. Logging
    5. Full-stack Frameworks
    6. Server-side Frameworks
      1. Node.js in Client-side Frameworks
    7. Web Publishing, Content Management Systems, and Blogging
    8. Headless Content Management Systems
    9. Static Site Generators
    10. Database Drivers
    11. Templating
    12. Command Line
    13. File System
    14. Network
    15. WebSockets
    16. Images
    17. Email
    18. Security and Authentication
    19. Summary
  25. Chapter 18: Node.js Application Deployment
    1. Pages vs Applications
    2. Node.js Application Preparation
    3. Dedicated Server Hosting
      1. sudo-not
      2. HTTP and HTTPS Considerations
      3. Process Management
        1. PM2 Port Clashes
      4. Use a Reverse Proxy
    4. Static Site Hosting (Jamstack)
      1. Build PHP-powered Static Sites with Node.js
    5. Serverless/Lambda Functions
      1. AWS Everywhere
    6. Container Hosting
    7. Summary
  26. Chapter 19: Epilogue
    1. Is Node.js for You?
    2. Is Deno Better?
    3. Thank You for Reading!
  27. Appendix: Quiz Answers
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12

Product information

  • Title: Node.js: Novice to Ninja
  • Author(s): Craig Buckler
  • Release date: June 2022
  • Publisher(s): SitePoint
  • ISBN: 9781925836523