# How Node.js Handles Multiple Requests with a Single Thread

# How Node.js Handles Multiple Requests with a Single Thread

You've probably heard this before — "Node.js is single-threaded." And your first thought might be, *wait, how does a single thread handle thousands of requests without crashing?* That's exactly what we're going to unpack. But first, let's get Node.js running on your machine so you can see things for yourself.

* * *

## 1\. Installing Node.js

Head over to [nodejs.org](https://nodejs.org) and download the LTS version. It works the same on Windows, macOS, and Linux — just follow the installer steps. LTS means "Long Term Support," which is basically the stable, boring version. That's the one you want.

* * *

## 2\. Checking installation using terminal

Once installed, open your terminal (or Command Prompt on Windows) and type:

```bash
node -v
npm -v
```

If you see version numbers, you're good. If not, restart your terminal and try again. Node.js comes with npm bundled in, so both should show up.

* * *

## 3\. Understanding Node REPL

REPL stands for **Read-Eval-Print Loop**. Think of it as a playground where you type JavaScript and Node.js instantly runs it.

Just type `node` in your terminal and hit Enter. You'll see a `>` prompt. Try this:

```js
> 2 + 2
4
> "Hello from Node!"
'Hello from Node!'
```

This is useful for quick experiments. To exit, press `Ctrl + C` twice, or type `.exit`.

The REPL is also where you start understanding Node's event loop — every line you type gets picked up, executed, and returned. That's the core idea behind how Node handles requests too.

* * *

## 4\. Creating first JS file

Create a file called `app.js` anywhere on your machine and add this:

```js
console.log("Node is running");
console.log("This is my first JS file");
```

Simple. Two lines. Nothing fancy. You're not in a browser anymore — there's no `window` or `document`. Just JavaScript running on your machine.

* * *

## 5\. Running script using node command

Open your terminal, navigate to where `app.js` is saved, and run:

```bash
node app.js
```

You'll see both lines printed instantly. That's Node executing your file top to bottom — synchronously. One line, then the next.

But here's where things get interesting. Node doesn't *stay* synchronous when it hits something slow, like a database call or a file read. It hands that off to the background (using something called libuv) and moves on. That's the secret to the single thread handling multiple requests — it doesn't wait around.

* * *

## 6\. Writing Hello World server

Now let's build something real. Open `app.js` and replace everything with this:

```js
const http = require("http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello World\n");
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});
```

Run it with `node app.js`, then open your browser and go to `http://localhost:3000`. You'll see **Hello World**.

This server is handling every request with that one callback function. No framework. No extra setup. And it's all running on a single thread — yet it can handle many requests at once because each request is non-blocking. While one request waits for data, Node is already processing the next one.

* * *

That's the whole trick. Node.js doesn't handle multiple requests by spinning up multiple threads. It handles them by *never sitting idle*. The event loop keeps picking up tasks, and slow work gets delegated to the background. One thread, but always busy.

Once this clicks, everything else in Node.js starts to make sense.
