Chapter 22: Node.js HTTP Module

1. What is the http module really?

node:http is the lowest-level built-in module in Node.js that lets you:

  • Create HTTP servers (listen for incoming requests)
  • Make HTTP client requests (talk to other servers)

It is the foundation that almost every higher-level framework (Express, Fastify, NestJS, Hono, Koa, etc.) is built on top of.

Important facts 2025–2026:

  • http is purely core — no npm install needed
  • It is very fast and very low overhead
  • It has no middleware, no routing, no JSON parsing — you get raw incoming requests and raw responses
  • Almost no one uses http directly for real applications anymore — but understanding it deeply makes you much better at using Express/Fastify/etc.

2. Creating the most basic HTTP server

Let’s start with the classic “hello world” server — but we’ll explain every line.

JavaScript

Run:

Bash

Open browser → http://localhost:3000

You should see:

text

3. Understanding the request & response objects

request (IncomingMessage)

Important properties you use almost every time:

JavaScript

Very common pattern — parsing query string

JavaScript

response (ServerResponse)

Important methods:

JavaScript

4. Realistic example: JSON API server (closer to real usage)

JavaScript

Try with curl:

Bash

5. Modern alternative: using fetch as client (Node.js 18+)

You don’t always need http.request anymore — you can use the global fetch:

JavaScript

Still useful to know http.request for:

  • Fine-grained control (custom agents, keep-alive, timeouts…)
  • HTTP/2 support
  • Streaming responses
  • Very low-level debugging

6. Important gotchas & best practices (2025–2026)

  1. Always call response.end() → if you forget → client hangs forever
  2. Body is streamed — you must collect chunks manually (or use express.json())
  3. Headers must be set before body → writeHead or setHeader before write/end
  4. Use node: prefix
    JavaScript
  5. Handle errors globally
JavaScript
  1. Do NOT block the event loop in request handler → long synchronous work → blocks all other requests
  2. Real apps almost never use raw http → Express/Fastify/Hono give you:
    • routing
    • middleware
    • JSON parsing
    • error handling
    • request body parsing
    • much cleaner code

Summary – When to use what

Use case Recommended choice 2026 Why?
Learning how HTTP works Raw node:http You understand the foundation
Building real APIs / websites Express, Fastify, Hono 100× faster development, safer
Very high-performance / minimal Fastify or Hono Very fast, low overhead
Microservices with strict control Raw http + undici or node-fetch Full control over headers, agents, timeouts
Making outgoing requests fetch (built-in since Node 18) Cleanest & modern

Which direction would you like to go deeper next?

  • Deep dive into streaming with http (file download, video, large responses)
  • Making HTTP client requests with http.request (very detailed)
  • Comparison: raw http vs Express vs Fastify vs Hono
  • How keep-alive and HTTP/2 work in Node.js
  • Error handling patterns in raw HTTP servers

Just tell me what interests you most — I’ll continue with concrete examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *