Fundamentals of Node.js for Developers

Jul 31, 2024

Node.js Lecture Notes

Introduction to Node.js

  • Node.js: Open source, cross-platform runtime environment for executing JavaScript outside the browser.
  • Commonly used for building back-end services (APIs) that power client applications (web apps, mobile apps).
  • Client applications interact with server services for data storage, email, notifications, etc.
  • Suitable for building highly scalable, data-intensive, and real-time back-end services.

Comparison with Other Tools

  • Alternatives: ASP.NET, Ruby on Rails, Django.
  • Advantages of Node.js:
    • Easy to start; good for prototyping and agile development.
    • Fast and scalable services in production (PayPal, Uber, Netflix, Walmart).
    • PayPal case study: Node app was built twice as fast, with fewer lines of code and files, doubling requests served per second while decreasing response time by 35%.

JavaScript for Full-Stack Development

  • If you know JavaScript (front-end), you can easily transition to back-end with Node.js.
  • Consistent source code: same naming conventions, tools, and best practices across front-end and back-end.
  • Large ecosystem of open-source libraries available for building applications quickly.

Node.js Architecture

  • Runtime Environment: Specifically designed to execute JavaScript code; uses Google's V8 engine.
  • Key Objects: Different from browser objects (e.g., lacks window/document) but provides file system and network capabilities.

Asynchronous vs. Synchronous

  • Asynchronous (Non-blocking): Handles multiple requests efficiently. Example: Restaurant waiter takes orders and serves multiple tables without waiting for food.
  • Synchronous (Blocking): Waits for one request to complete before handling the next, leading to inefficient resource use (common in traditional frameworks like ASP.NET).
  • Node.js handles requests asynchronously by using a single thread and an event queue for processing.

Limitations of Node.js

  • Not suitable for CPU-intensive applications (e.g., video encoding, image processing) due to single-threaded nature.
  • Best used for data-intensive and real-time applications.

Installing Node.js

  1. Check Node.js version: node --version
  2. Install the latest stable version from the official Node.js website.
  3. Verify installation with node --version.

Creating Your First Node Application

  • Create a folder for your app and open it in a code editor (e.g. Visual Studio Code).
  • Write JavaScript code in an app.js file (example function: sayHello(name)).
  • Run the application using: node app.js.

Node.js Module System

  • Modules: Encapsulates code in Node.js; prevents pollution of global scope.
  • Every file is a module; variables/functions are scoped to the module.
  • Use module.exports to expose functionalities.

Global Objects in Node.js

  • Node.js provides global objects such as console, setTimeout, and setInterval.
  • Global Scope: Variables/functions defined are not added to the global object unless explicitly exported.

Creating and Loading Modules

  • Create a new module (e.g., logger.js) to manage logging.
  • Use require to load modules and use their functionality.
  • Module encapsulation prevents namespace collisions.

Event-Driven Architecture

  • Node.js uses an event-driven architecture to handle asynchronous operations.
  • Event Emitter: Core building block for handling events in Node.js.
  • Register listeners to handle specific events (e.g., emitter.on('eventName', callback)).

HTTP Module in Node.js

  • Built-in HTTP module: Used for creating web servers.
  • Example: Create an HTTP server that listens on a port and handles requests.
  • Use http.createServer() to create a server and register request handlers.

Summary

  • Node.js is a powerful tool for building scalable back-end applications using JavaScript.
  • Understanding Node.js architecture and modules is crucial for effective back-end development.
  • Next steps include deeper exploration of built-in modules and building RESTful APIs.