Skip to main content
Node.js Basics
CHAPTER 01 Beginner

Introduction to Node.js

Updated: Aug 1, 2026
10 min read

# Introduction to Node.js

Welcome to Chapter 1 of the Node.js Basics course! If you're a beginner to backend development, or a JavaScript developer looking to expand your skills to the server side, you are in the right place. In this chapter, we will lay the foundation by understanding exactly what Node.js is, how it works, and why it has become an industry standard for backend development.

---

For the first fifteen years of its life, JavaScript could only run in one place: a web browser. If you wanted to write server code you learned a second language — PHP, Java, Python, Ruby — and maintained a permanent mental context switch between the two halves of your own application.

Node.js ended that in 2009 by taking Chrome's JavaScript engine, V8, out of the browser and wrapping it in everything a server needs: file system access, networking, process control. The language did not change. The environment did.

The consequence that matters is not really "one language everywhere," pleasant though that is. It is Node's concurrency model. A traditional server assigns each incoming request a thread, and a thread that is waiting on a database is a thread doing nothing while consuming memory. Node instead runs a single thread with an event loop: work that waits — disk reads, network calls, database queries — is registered with a callback and set aside, and the thread immediately picks up the next request. Thousands of simultaneous connections, most of them idle at any given instant, cost remarkably little.

That design makes Node excellent at I/O-heavy work such as APIs, proxies and real-time services, and genuinely poor at CPU-heavy work such as video encoding, which blocks the one thread everything else depends on. Knowing which side of that line your problem falls on is the most useful thing this course can teach you.

We begin by installing Node and understanding the runtime before writing a single server.

1. Beginner-Friendly Explanations

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime environment.

Let's break that down:

  • Open-source: It's free, and its code is public. Thousands of developers contribute to it.
  • Cross-platform: It runs on Windows, macOS, and Linux.
  • Runtime environment: It provides the necessary tools and environment to run JavaScript outside of a browser. It is not a programming language or a framework; it's a runtime built on Chrome's V8 JavaScript engine.

History of Node.js

Node.js was created by Ryan Dahl in 2009. He was frustrated with how existing web servers (like Apache) handled multiple concurrent connections. They would create a new thread (a separate process) for every request, which consumed a massive amount of memory. Ryan Dahl took Chrome's extremely fast V8 JavaScript engine and embedded it inside a C++ program, adding an event loop to handle concurrent requests efficiently without creating new threads for each one.

Why use Node.js?

Node.js is insanely popular for several reasons:
  1. 1. JavaScript Everywhere: You only need to know JavaScript for the whole stack.
  1. 2. Extremely Fast: Built on Google Chrome's V8 engine, which compiles JavaScript into machine code directly.
  1. 3. Asynchronous & Non-blocking: Node.js doesn't wait for tasks (like reading a file or fetching database data) to finish. It moves on to the next task and handles the previous one when it's done.
  1. 4. Massive Ecosystem: The Node Package Manager (NPM) is the largest software registry in the world, offering over a million free libraries.

---

2. Syntax Explanation

Since Node.js *is* JavaScript, the syntax is identical to what you might write for the browser, but with some extra global objects and without browser-specific objects (like window or document).

``javascript id="ch1-syntax-1" // Typical JavaScript variables and functions work exactly the same const serverName = "My First Server";

function greet(name) { return Welcome to ${name}`; }

// Logging to the console works the same way console.log(greet(serverName));

12345678910111213141516171819202122232425
**Output Explanation:**
When this code is executed in Node.js, the V8 engine reads it and logs `Welcome to My First Server` to the terminal or console. 

---

## 3. Real-world Examples

### Backend Development Overview
Imagine a restaurant:
- **Frontend (Client):** The menu, the tables, the ambiance. It's what the customer sees and interacts with.
- **Backend (Server):** The kitchen. Customers don't see it, but it's where the food is prepared, ingredients are fetched from the fridge (Database), and orders are fulfilled.

Node.js is like a super-efficient kitchen manager who handles thousands of orders simultaneously without getting overwhelmed, ensuring every order is processed and sent back to the customer efficiently.

Companies using Node.js include **Netflix, PayPal, LinkedIn, Uber, and NASA**. LinkedIn switched their mobile app backend from Ruby on Rails to Node.js, reducing their servers from 30 to 3 and making the app 20 times faster!

---

## 4. Multiple Code Examples

Let's look at how Node.js differs slightly from browser JavaScript.

### Example 1: Global Object
In a browser, the global object is `window`. In Node.js, it's `global`.

javascript id="ch1-code-1" // In browser: window.setTimeout() // In Node.js: global.setTimeout()

global.setTimeout(() => { console.log("This runs after 1 second."); }, 1000);

123
### Example 2: Understanding Non-Blocking Nature
Here is a simulation of Node's non-blocking nature.

javascript id="ch1-code-2" console.log("1. Requesting data from database...");

setTimeout(() => { console.log("2. Data received from database!"); }, 2000); // Simulates a 2-second delay

console.log("3. Doing other things while waiting...");

1
**Output Explanation:**
  1. 1. Requesting data from database...
  1. 3. Doing other things while waiting...
  1. 2. Data received from database!
12345
Notice how Node.js didn't wait for the 2 seconds to finish before printing step 3. This is what makes Node.js fast for web servers.

### Example 3: Process Information
Node.js gives you access to the current system process.

javascript id="ch1-code-3" // Print the current version of Node.js console.log(Node.js Version: ${process.version});

// Print the platform (e.g., win32, darwin, linux) console.log(Operating System: ${process.platform});

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
---

## 5. Output Explanations

Whenever you run JavaScript in a browser, the output usually affects the DOM (the web page). In Node.js, your output is typically directed to the **terminal** or sent as an HTTP response to a client.

In the previous examples, functions like `console.log()` print text directly to the command line interface (CLI) where the Node script was executed.

---

## 6. Common Mistakes

1. **Trying to use DOM methods:** Beginners often try to use `document.getElementById()` or `window.alert()` in Node.js. This will throw an error because Node.js doesn't have a graphical interface; it's a server environment.
2. **Confusing Node.js with a framework:** Node.js is a runtime. Frameworks like Express.js are built *on top* of Node.js.
3. **Blocking the Event Loop:** Writing heavy, synchronous code (like complex math calculations in a massive loop) will block Node.js from handling other requests because it is single-threaded.

---

## 7. Best Practices

- **Think Asynchronously:** Always embrace non-blocking, asynchronous functions when dealing with file systems, databases, or network requests.
- **Understand the Event Loop:** Having a solid mental model of how the Event Loop handles tasks is crucial for writing efficient backend code.
- **Keep Functions Small:** Small, focused functions are easier to test and don't block the execution thread for long.

---

## 8. Node.js Architecture & Single-threaded Event Loop

How can Node.js handle millions of concurrent connections if it only has one thread? 

**The Event Loop:**
Node.js operates on a **Single-Threaded Event Loop** architecture.
1. When a client sends a request to a Node.js server, Node places it in an Event Queue.
2. The Event Loop continuously checks this queue.
3. If the task is simple (like returning a string), Node handles it immediately.
4. If it's complex (like reading a file or querying a database), Node passes the task to worker threads in the background (managed by a C++ library called `libuv`).
5. The main thread immediately moves on to the next request without waiting.
6. When the background task finishes, it signals the Event Loop, and the result is sent back to the client.

This design is incredibly lightweight and efficient compared to older server models.

---

## 9. Mini Project: Print welcome message in terminal

Let's write a very simple Node.js script that acts as our first program.

**Objective:** Create a script that prints a personalized welcome message along with some system information.

**Code:**

javascript id="ch1-mini-project" // welcome.js

const userName = "Future Backend Developer"; const currentDate = new Date().toDateString();

console.log("========================================="); console.log(🌟 Welcome to Node.js, ${userName}!); console.log(📅 Today is: ${currentDate}); console.log(💻 You are running Node version: ${process.version}); console.log("🚀 Get ready to build awesome backends!"); console.log("========================================="); ``

How to run (Preview for next chapter): You would save this as welcome.js and run it in your terminal using the command: node welcome.js.

---

10. Coding Challenges

Challenge 1: Non-Blocking Test Write a script that prints "Start", uses a setTimeout of 0 milliseconds to print "Timeout", and then prints "End". Predict the order before running it.

Challenge 2: System Specs Use the process object to log the architecture of your CPU (hint: look up process.arch).

---

11. MCQs with Answers

Q1: What exactly is Node.js? A) A JavaScript framework B) A JavaScript runtime environment C) A new programming language D) A database management system Answer: B

Q2: Which engine powers Node.js? A) SpiderMonkey B) Chakra C) V8 D) WebKit Answer: C

Q3: Node.js is primarily used for: A) Designing web pages B) Server-side scripting C) Machine learning D) Video editing Answer: B

Q4: Is Node.js multi-threaded or single-threaded by default? A) Multi-threaded B) Single-threaded Answer: B

---

12. Interview Questions

  1. 1. What is Node.js and why is it used?
*Answer:* It's an open-source, cross-platform JavaScript runtime built on Chrome's V8 engine. It allows developers to run JavaScript on the server side to build scalable backend applications.
  1. 2. Explain the difference between Node.js and JavaScript.
*Answer:* JavaScript is a programming language. Node.js is a runtime environment that allows JavaScript code to execute outside of a browser.
  1. 3. What is meant by "non-blocking" in Node.js?
*Answer:* Non-blocking means the application does not wait for I/O operations (like reading a file or network requests) to complete before moving on to the next line of code.

---

13. FAQs

Q: Do I need to know JavaScript before learning Node.js? A: Yes. You should have a solid understanding of basic JavaScript concepts like variables, functions, loops, and callbacks/promises.

Q: Can Node.js be used for frontend development? A: No, Node.js is strictly for backend development, though frontend tools (like React or Vue) use Node.js to build and bundle their code.

Q: Is Node.js dead? A: Absolutely not! It is one of the most widely used backend technologies in the world, constantly updated and heavily supported by the open-source community.

---

14. Summary

  • Node.js allows JavaScript to run on the server side.
  • It is built on Google Chrome's highly optimized V8 engine.
  • Its biggest strength is its asynchronous, non-blocking, single-threaded event loop architecture, which makes it highly scalable and fast.
  • Node.js doesn't have access to browser-specific objects like window or document`.
  • It's heavily used by massive enterprise companies for building APIs and microservices.

---

15. Next Chapter Recommendation

Now that you know what Node.js is and how it works under the hood, it's time to actually get it running on your computer. In Chapter 2: Installing Node.js and Setting Up Environment, we will guide you step-by-step through installing Node.js, setting up VS Code, and running your very first script!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·