Frameworks & Libraries

Node.js Introduction

Node.js brings JavaScript to the server. Built on Chrome's V8 engine with a non-blocking, event-driven architecture — it's perfect for APIs, real-time apps, CLI tools, and anything that needs fast I/O.

What is Node.js?

Node.js is a runtime that lets JavaScript run outside the browser, on servers, in command-line tools and in build scripts. It uses Chrome's V8 engine and adds APIs for files (fs), networking (http), processes and more, with a non-blocking, event-driven design that handles many connections efficiently.

// server.js
import { createServer } from "node:http";

createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ ok: true }));
}).listen(3000);

// terminal
node server.js

Core Built-in Modules

fs / fs/promises

File read, write, delete, watch, stat

path

join, resolve, basename, dirname, extname

http / https

Create HTTP servers, make requests

events

EventEmitter — pub/sub in Node.js

stream

Readable, Writable, Transform, pipeline

crypto

Hash (SHA256), HMAC, random bytes, UUID

os

cpus, platform, homedir, totalmem

child_process

exec, spawn — run shell commands

worker_threads

True parallelism for CPU-intensive tasks

net

TCP server/socket (low-level networking)

url / querystring

Parse and build URLs and query strings

timers/promises

setTimeout/setInterval as Promises

Common Pitfalls

Blocking the event loop with synchronous operations: fs.readFileSync() in a web server — blocks ALL requests
Always use async versions in servers: await fs.promises.readFile(). Reserve sync for startup scripts.
Not handling 'unhandledRejection' or 'uncaughtException' — silently crashes the process in production
process.on('unhandledRejection', ...) and process.on('uncaughtException', ...) + proper error handling in async code.
Hardcoding secrets (API keys, DB passwords) in source code
Use process.env with a .env file (dotenv) and add .env to .gitignore. Never commit secrets.
Using require() with user-supplied paths — path traversal attack (../../etc/passwd)
Validate and sanitize all file paths. Use path.resolve() + check it starts within expected directory.

Interactive Demo

Code Editor

<p>Node.js runs on the server, not in this preview. Here is a minimal server:</p>
<pre>// server.js   →   run with: node server.js
import {
  createServer,
} from "node:http";

createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ message: "Hello from Node", path: req.url }));
}).listen(3000, () => console.log("http://localhost:3000"));</pre>
<p>Visiting <code>http://localhost:3000/hi</code> would return:</p>
<pre id="out"></pre>

Preview

Node.js Introduction: summary

Key points

  • There's no DOM or window in Node.js; the global object is globalThis.
  • Prefer async APIs (fs/promises) over sync ones in servers so requests aren't blocked.
  • Use LTS (even-numbered) versions in production.
  • Modern Node has built-in fetch, a test runner and --watch mode.

Frequently asked questions

›Is Node.js a programming language?

No. It's a runtime environment that executes JavaScript outside the browser.