No history yet

Introduction to Node.js

JavaScript Beyond the Browser

For a long time, JavaScript lived exclusively inside web browsers. Its main job was to make websites interactive—things like dropdown menus, animations, and form validation. If you wanted to build the part of an application that runs on a server, you had to use a different language, like Python, Ruby, or Java.

Node.js changed all of that. It's not a new language, but rather a runtime environment. Think of it as a special container that allows JavaScript code to run on a computer directly, without needing a web browser. This means you can use the same language you use for the website's front end to build its powerful back end.

Node.js is an open-source and cross-platform Javascript Runtime Environment.

It’s built on Chrome's V8 engine, the same high-performance engine that powers Google Chrome. By taking JavaScript out of the browser, Node.js opened the door for developers to build fast, scalable server-side applications all with one language.

Running Your First Script

Getting started is straightforward. First, you'll need to install Node.js from its official website, nodejs.org. The installer includes both Node.js itself and npm (Node Package Manager), a tool for managing project libraries that you'll use extensively.

Once installed, you can run a JavaScript file from your computer's terminal or command prompt. Let's create a simple file named app.js.

// app.js

const message = "Hello from the server!";
console.log(message);

This code doesn't do anything fancy. It just declares a variable and prints it to the console. To run it, open your terminal, navigate to the directory where you saved app.js, and type the following command:

node app.js

You should see the output Hello from the server!. Congratulations, you've just executed JavaScript on a server environment.

Lesson image

The Event-Driven Engine

One of the most powerful features of Node.js is its event-driven, non-blocking architecture. This sounds complex, but the idea is simple and efficient.

Imagine a waiter at a restaurant. A traditional, blocking server is like a waiter who takes one customer's order, walks it to the kitchen, waits for the food to be cooked, and only then delivers it before taking the next order. If the kitchen is slow, everyone else has to wait.

Node.js works like a more efficient waiter. It takes an order (a request), gives it to the kitchen (a database query, file read, etc.), and immediately moves on to the next customer. When the kitchen finishes a dish, it places it on the counter (an event). The waiter is notified and then delivers the food. This way, the waiter is never just standing around waiting and can serve many tables at once.

This non-blocking approach makes Node.js incredibly fast and efficient for applications that handle many simultaneous connections, such as chat apps, streaming services, and APIs.

Key Advantage: Using JavaScript on both the front end and back end simplifies development. A single team can manage the entire application, and code can even be shared between the client and server.