1. Introduction to Node.js
What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 engine. Before Node.js, JavaScript could only run inside web browsers. Ryan Dahl took the V8 engine, wrapped it in a C++ program, and gave JavaScript the ability to read files, listen on network ports, and talk to databases. This meant JavaScript could finally run on servers.
🌍 Why Node.js Became Popular
Traditional servers like Apache create a new thread for every incoming request. If 10,000 users connect, you need 10,000 threads, and memory usage explodes. Node.js uses a single thread with an event loop. It can handle thousands of concurrent connections using very little memory. This makes Node.js perfect for APIs, real-time applications like chat, and data streaming services.
However, Node.js is not ideal for CPU-intensive work like image processing or complex math. If the single thread gets stuck calculating, the entire server freezes. For those tasks, you use Worker Threads or separate microservices.
🔧 Installing Node.js
You can download Node.js directly from nodejs.org. The LTS (Long Term Support) version is recommended for production because it receives security updates for years. Professional developers prefer using nvm (Node Version Manager), which lets you switch between Node versions instantly because different projects often require different versions.
🚀 Your First Node.js Program
Create a file called app.js. Write a single line of JavaScript. Run it with the node command. That's it. Node.js executes your code just like a browser would, but without any HTML or CSS around it.
This is running on my computer, not a browser.
Try it: Change the message and run the file again. The program ends immediately after printing. That's normal for simple scripts.
📁 The Global Object: global vs window
In a browser, the global object is called window. Any variable declared with var becomes attached to window. In Node.js, the global object is called global. But here's the key difference: variables you create in a Node.js file are NOT added to global. They stay private to that specific file. This prevents naming conflicts between different files.
This is a secret
📂 Useful Globals: __dirname and __filename
Node.js provides two extremely useful global variables. __dirname gives you the absolute path to the folder containing the current file. __filename gives you the full path including the file name. These are essential when you need to read or write files relative to your project location.
This folder is at: /Users/name/project
⏳ The process Object
Unlike a browser script that runs and disappears, a Node.js server stays alive waiting for events. The process object gives you control over this lifecycle. You can read environment variables, exit the program, or listen for events like SIGTERM (the signal that tells your app to shut down).
📁 Professional Project Structure
Senior engineers organize their projects from day one. Here is the standard enterprise layout that keeps your code maintainable as it grows.
Knowledge Check
Ready to test your understanding of 1. Introduction to Node.js?