Lesson

Node.js Intro

10 min reading
Free Course

Node.js Intro: Server-Side JavaScript and the Event Loop

Node.js is an open-source, cross-platform JavaScript runtime built on Google Chrome's V8 engine. It lets developers run JavaScript outside the browser to build fast REST APIs, real-time WebSocket servers, CLI tools, and microservice backends using one language across the stack.

How the Node.js Event Loop Handles Concurrency

Traditional blocking servers assign a separate operating-system thread to wait on every database query or file read. Node.js uses a single main JavaScript thread paired with a non-blocking event loop (libuv) that offloads network and disk I/O to the kernel and invokes callbacks or promises only when data arrives.

flowchart LR
    A["Incoming Requests"] --> B["Node.js Event Loop"]
    B -->|"Non-blocking I/O"| C["Database / File System / Network"]
    C -->|"Completion Callback"| D["Callback / Microtask Queue"]
    D --> B
    B --> E["HTTP Responses"]

Because Node.js never pauses waiting on idle I/O sockets, one server process easily serves thousands of concurrent API requests, chat streams, or proxy connections with low memory overhead.

What Node.js Builds Best (and When to Avoid It)

Node.js shines for I/O-heavy workloads such as JSON APIs, Next.js server rendering, real-time dashboards, and developer CLIs. For heavy CPU tasks like video encoding or large matrix math, offload work to Worker Threads or a compiled native service so the event loop stays responsive.

import http from 'node:http';

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({ service: 'api', status: 'ok', path: req.url }));
});

server.listen(3000, () => {
  console.log('Node.js server listening on http://localhost:3000');
});

Common Mistakes to Avoid

  • Blocking the event loop with synchronous calls: Calling fs.readFileSync() or running massive while loops inside an HTTP route freezes every other connected user until the work finishes.
  • Expecting browser globals in Node.js: window and document do not exist on the server; Node.js exposes process, Buffer, and built-in node: modules instead.
  • Swallowing unhandled promise rejections: Always handle async errors with try / catch around await calls so failed queries return clean HTTP 500 responses instead of crashing the process.

Frequently Asked Questions

Is Node.js a programming language or a framework?

Node.js is a JavaScript runtime environment, not a language or web framework. Frameworks like Express, Fastify, and NestJS run on top of Node.js.

Does Node.js support fetch() natively?

Yes. Node.js 18+ includes standard global fetch(), Request, and Response APIs without installing third-party HTTP libraries.

Why do Node.js core imports start with node:?

The node: prefix (like import fs from 'node:fs/promises') tells Node explicitly to load a built-in module rather than searching node_modules.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Node.js Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum