No history yet

Introduction to Node.js

JavaScript Beyond the Browser

For a long time, JavaScript lived in a specific neighborhood: the web browser. It was the language that made websites interactive, handling things like pop-up alerts, animations, and form submissions. If you wanted to build the part of an application that runs on a server—handling databases, user accounts, and business logic—you had to switch to another language like Python, Ruby, or Java. This meant developers often needed to be fluent in multiple languages to build a complete web application.

Node.js is a JavaScript runtime environment that allows developers to run JavaScript on the server side.

In 2009, that changed. A developer named Ryan Dahl created Node.js, a program that lets you run JavaScript code directly on your computer, completely outside of a web browser. Think of a runtime environment as a self-contained system that provides everything a programming language needs to operate. By creating one for JavaScript that could run on a server, Node.js gave developers the ability to use the same language for both the front-end (what the user sees) and the back-end (the server-side logic). This was a game-changer.

The Non-Blocking Model

The real magic of Node.js is its architecture. It uses an event-driven, non-blocking I/O model. That sounds complex, but the idea is simple. Let's compare it to a restaurant.

In a traditional, blocking model, a waiter takes an order from one table, goes to the kitchen, and waits there until the food is ready before serving it. Only then do they move on to the next table. If the kitchen is slow, the waiter is stuck, and other customers are left waiting.

Node.js works like a more efficient waiter. In this non-blocking model, the waiter takes an order, gives it to the kitchen, and immediately moves on to the next table to take their order. When the kitchen finishes a dish, it rings a bell (an "event"), and the waiter knows to pick it up and deliver it. The waiter never just stands around waiting; they are always handling new requests.

This approach makes Node.js extremely efficient for applications that handle many connections at once, like chat applications, live-streaming services, and collaborative tools. It can serve thousands of users simultaneously without getting bogged down waiting for tasks like database queries or file reads to complete.

Installing Node.js

Getting Node.js on your machine is straightforward. It comes with npm (Node Package Manager), a powerful tool for installing and managing reusable code packages. You'll use npm all the time as a Node.js developer.

On Windows and macOS:

  1. Go to the official Node.js website: https://nodejs.org/
  2. You'll see two download options: LTS and Current. LTS stands for Long-Term Support. It's the most stable version and is recommended for most users.
  3. Download the installer for your operating system and run it. The installation wizard will guide you through the process.

On Linux: While you can download it from the website, it's often easier to use a version manager like nvm (Node Version Manager). This tool lets you install and switch between different Node.js versions easily.

  1. Open your terminal and install nvm using the command from its official GitHub page (it's usually a curl or wget command).
  2. Close and reopen your terminal.
  3. Install the latest LTS version with the command:
nvm install --lts

To verify that Node.js and npm are installed correctly, open your terminal (or Command Prompt on Windows) and run these two commands:

node -v
npm -v

Each command should print a version number. If they do, you're all set! You've successfully installed Node.js and are ready to start building.

Quiz Questions 1/6

What was the primary limitation of JavaScript before the creation of Node.js in 2009?

Quiz Questions 2/6

The architecture of Node.js is described as an "event-driven, non-blocking I/O model." In the restaurant analogy, what does the "event" correspond to?