The Node.js HTTP Module

๐ŸŸข Node.js LTS ๐Ÿ“— Chapter 18 of 49 ๐Ÿ“‚ Phase 6: HTTP & Web Servers ๐Ÿ—“๏ธ 2026 Edition
๐Ÿ“Œ Covered in this chapter: createServer() ยท Request & Response Objects ยท JSON Responses ยท Basic Route Handling

Build a raw web server from scratch using Node's built-in http module โ€” no framework required.

1The Node.js HTTP Module โ€” What You'll Learn

Build a raw web server from scratch using Node's built-in http module โ€” no framework required.

Here's everything this chapter covers, in the order you'll learn it:

  • The node:http module
  • Creating a server with createServer()
  • The request object
  • The response object
  • Reading the request method
  • Reading the request URL
  • Setting headers
  • Setting status codes
  • Sending a JSON response
  • Basic route handling
  • Reading the request body
  • A minimal CRUD server without a framework
2Working Example
๐Ÿ’ป Example: The Node.js HTTP Module
import { createServer } from "node:http";

const server = createServer((request, response) => {
  response.writeHead(200, {
    "Content-Type": "application/json",
  });

  response.end(JSON.stringify({
    message: "Hello from Node.js",
  }));
});

server.listen(3000, () => {
  console.log("Server running on port 3000");
});
3Best Practices & Common Pitfalls
๐Ÿ’ก Key things to remember:
  • Forgetting to call response.end() is one of the most common bugs โ€” the client's request will hang forever waiting for a response.
  • writeHead(200, {...}) sets both the status code and headers in a single call, and must happen before you write the body.
โ“ Frequently Asked Questions (FAQ)

Q What's the most important thing to understand about the node.js http module?

Focus on: createServer() ยท Request & Response Objects ยท JSON Responses ยท Basic Route Handling. These are the core building blocks this chapter's examples are built around, and they show up repeatedly in later chapters of this course.

Q Do I need external npm packages for the node.js http module?

Only where explicitly shown in the code examples above (like Express, Zod, or Socket.IO) โ€” otherwise, this chapter relies entirely on Node.js's own built-in capabilities.

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Node.js LTS ยท Last updated August 2026