← Node.js from Scratch to Advanced
Lesson 1 of 3

What Is Node.js, and Why Use It?

SoftwareBeginner

JavaScript, Outside the Browser

Node.js is a runtime that lets you execute JavaScript outside a web browser — on a server, in a CLI tool, anywhere. It's built on Chrome's V8 engine, the same one that runs JavaScript in Chrome, embedded into a standalone program.

javascript
const http = require('http');

http.createServer((req, res) => {
  res.end('Hello from Node.js');
}).listen(3000);

You don't need Express or any framework to start a server — Node.js ships an HTTP server in its standard library, similar to Go's net/http.


The rest of this series builds up from this single-file server to a structured, production-shaped Node.js application.

Where You'll Actually Use This

In a real job, you almost never sit down to "write a Node.js app" in the abstract. You write a small script that renames a batch of files, a webhook handler that reacts when someone pushes to a repo, a health-check endpoint a load balancer pings every ten seconds, or a CLI tool a teammate runs before opening a pull request. Node is popular for exactly this kind of glue work because starting it up costs almost nothing - no separate web server to install, no compiler step, just `node script.js`.

A common mistake early on: assuming Node.js and Express (or another framework) are the same thing. They are not. Node is the runtime - the thing that executes JavaScript outside a browser. Express is a library that runs on top of Node to make building HTTP servers less repetitive. You can write a complete, working server in plain Node with zero dependencies, as the example above shows - frameworks are a convenience, not a requirement.

The Event Loop, Briefly

The reason Node can handle thousands of simultaneous connections on a single thread comes down to one design decision: almost everything that talks to the outside world - reading a file, querying a database, making an HTTP request - is non-blocking. Instead of freezing the whole program while it waits for a disk or network response, Node hands the operation off and moves on to other work, then comes back to your code when the result is ready.

javascript
// Blocking - nothing else runs until the file is fully read
const data = fs.readFileSync('big-file.txt');
console.log('done reading');

// Non-blocking - Node moves on immediately, runs the callback later
fs.readFile('big-file.txt', (err, data) => {
  console.log('done reading');
});
console.log('this line runs first');

That's the whole idea behind "the event loop" - it's not magic, it's a queue. Node keeps working through whatever code is ready to run, and when a non-blocking operation finishes, its callback gets added to that queue. This is also exactly why one slow, CPU-heavy calculation (not I/O - actual number crunching) can freeze a Node server for every user at once: there's still only one thread doing the JavaScript execution itself.