No history yet

Introduction to Node.js

JavaScript Beyond the Browser

For a long time, JavaScript lived exclusively inside web browsers. Its job was to make websites interactive—things like validating forms, creating animations, and loading new data without a full page refresh. If you wanted to build the part of an application that runs on a server, you had to use a different language, like Python, Java, or PHP.

Node.js changed all that. It's a runtime environment that allows you to run JavaScript code directly on a computer, completely outside of a web browser. This means you can use the same language for both the front-end (what the user sees) and the back-end (the server logic).

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to use JavaScript on the server-side.

At its heart, Node.js uses the V8 engine, the same high-performance engine that powers the Google Chrome browser. This makes it incredibly fast at executing JavaScript code. But Node.js adds a crucial set of tools for server-side tasks, like interacting with the file system, managing network connections, and communicating with databases.

The Event-Driven Model

Traditional server-side technologies often follow a multi-threaded model. Imagine a busy restaurant with one waiter assigned to each table. A waiter takes an order from Table 1, walks it to the kitchen, and waits there until the food is ready before serving it. Only then do they move on to Table 2. If the kitchen is slow, Table 2 waits a long time, even if they're just ready to order a drink. Each new table requires a new waiter, which consumes more resources (memory and processing power).

Node.js works differently. It's like having a single, super-efficient waiter. This waiter takes an order from Table 1 and gives it to the kitchen. Instead of waiting, the waiter immediately moves to Table 2 to take their order, then Table 3, and so on. When the kitchen finishes a dish, it places it on a counter and rings a bell—an event. The waiter hears the bell, picks up the finished dish, and delivers it to the correct table before resuming their rounds. This is the essence of a non-blocking, event-driven architecture.

This model makes Node.js highly efficient for applications that involve a lot of I/O (Input/Output) operations, such as web servers, APIs, and chat applications. It can handle thousands of concurrent connections with minimal overhead because it doesn't get "blocked" waiting for tasks like reading a file or querying a database to finish.

Setting Up Your Environment

Getting Node.js running on your machine is straightforward. The official website provides installers for Windows, macOS, and Linux.

  1. Go to the official Node.js website: https://nodejs.org/
  2. You will see two versions available for download: LTS and Current.
    • LTS (Long-Term Support): This is the recommended version for most users. It's stable, reliable, and will be supported with bug fixes and security updates for a long time.
    • Current: This version has the latest features, but it might not be as stable. It's great for experimenting with new additions to Node.js, but LTS is better for production work.
  3. Download the LTS installer for your operating system and run it, following the on-screen instructions.

To verify that Node.js has been installed correctly, open your terminal or command prompt and type the following command:

node -v

This should print the version number of Node.js that you just installed, something like v18.18.0. The installer also includes npm (Node Package Manager), a tool for managing third-party libraries. You can check its version too:

npm -v

Running Your First Script

Now that you have Node.js installed, let's write a simple script. Unlike browser JavaScript that interacts with HTML, Node.js scripts can do things like print directly to your terminal.

  1. Create a new file named app.js.
  2. Open the file in a text editor and add the following code:
// app.js

const greeting = "Hello, Node.js!";

console.log(greeting);

This is standard JavaScript. The console.log() function works just like it does in the browser's developer console. The key difference is where the output appears.

To run this file, navigate to the directory where you saved app.js in your terminal and execute the following command:

node app.js

You should see the text Hello, Node.js! printed directly in your terminal. You've just run your first server-side JavaScript program!

Quiz Questions 1/5

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

Quiz Questions 2/5

Node.js's architecture is often compared to a single, efficient waiter in a restaurant. What model does this analogy describe?