Node.js Training Session Notes

Jul 9, 2024

Online Training Session on Node.js Runtime

General Information

  • Beginner-level class on Node.js runtime
  • Interactive session, no slides, Sam Hernandez presenting
  • Recording available in YouTube and split by topic on GiasComplete
  • Questions can be asked on GiasComplete Slack channel
  • Lecture duration: 2.5 hours

Introduction

  • No assumptions about prior Node.js knowledge
  • Assumes basic JavaScript and general programming knowledge
  • Lecture's purpose: familiarizing with Node.js, raising confidence, correct learning pathway
  • Emphasis on understanding raw runtime before exploring external packages
  • Importance of using the latest version of Node.js (preferably LTS)

Instructor Background - Sam Hernandez

  • Experience in various programming languages before settling on Node.js
  • Backend Node.js development and frontend React development
  • Maintains JiS Complete written in Node.js and React

Getting Started with Node.js

  • Node.js consists of: node, npm, and npx commands
  • Node.js is a runtime built on Chrome's V8 JavaScript engine
  • Explore through REPL (Read-Eval-Print Loop): node command in terminal

Core Libraries and Features

Important Node.js Built-in Libraries

  • HTTP/HTTPS: Start and manage HTTP servers
  • Console: Similar to browser console for logging
  • Process: Interface between Node.js and OS for interacting with environment variables and process states
  • File System (fs): Read/write files
  • Path: Managing and creating paths
  • Events: Create and manage event-driven architecture
  • Streams: Work with large datasets
  • Crypto: Security and encryption
  • Timers: Handle time-based asynchronous operations

Node.js Versions and Installation

  • Use Homebrew on Mac for installations
  • Use NVM (Node Version Manager) for switching Node versions
  • Ensure minimum Node.js version 10 for compatibility

Modules and Packages

  • require and module.exports for modular code
  • Use npm for package management and package.json for meta information
  • export default and import ES6 modules vs CommonJS modules require/module.exports
  • Module caching to optimize performance

Debugging and Development Tools

  • Use Node.js CLI for various development tasks
  • Debugging tools: node inspect, Chrome DevTools integration for step-by-step debugging
  • ESLint for code linting and quality checks
  • TypeScript recommended for type safety and project scalability

Node.js Application Setup and Best Practices

  • Use package.json scripts section to manage project-specific tasks
  • Lifecycle scripts (e.g., start, test, build) to define project execution commands
  • Avoid global dependencies, manage locally within project

Starting an HTTP Server

  • Basic server using built-in HTTP module
  • Event-driven: Handle requests and responses via callback functions
  • Example: Create a simple server and handle routes

Express Framework for Simplified HTTP Handling

  • Express as a popular, feature-rich framework for HTTP servers
  • Installation via npm and setup examples
  • Route management, request handling, serving static files

Callbacks, Promises, and Async/Await

  • Callback pattern (error-first callback): Managing asynchronous operations, handling errors
  • Promises: Cleaner alternative to callbacks, better trust and control
  • Async/Await: Syntactic sugar over promises for better flow control
  • Promisify utility from util library

Advanced Topics

Streams and Event Emitters

  • Streams: Efficient data processing handling large datasets over time
  • Event Emitters: Observers for state changes and multiple event handling
  • Examples and use cases in typical Node.js applications

Performance and Scalability

  • Using Cluster module: Load balancing and zero-downtime restarts
  • Recommended: Use pm2 for process management in production

Further Resources and Learning

  • JiS Complete courses and books on Node.js
  • Pluralsight courses recommended for in-depth learning
  • Reading and understanding official Node.js documentation
  • Focus on mastering the core built-in module features

Additional Recommendations

  • Use ESLint and Prettier for maintaining code quality
  • Focus on understanding modern JavaScript features: Arrow functions, destructuring, spread/rest operators, modules
  • Familiarity with TypeScript and its benefits

Questions and Answers

  • Discussion on general practices, avoiding bad parts of JavaScript
  • Recommendations for using strict mode, TypeScript, and linting tools to enforce code quality