Routing Without a Framework

๐ŸŸข Node.js LTS ๐Ÿ“— Chapter 19 of 49 ๐Ÿ“‚ Phase 6: HTTP & Web Servers ๐Ÿ—“๏ธ 2026 Edition
๐Ÿ“Œ Covered in this chapter: URL Pathname Matching ยท Method Checking ยท Query Parameters ยท 404 Handling

Build simple GET/POST route handling using only Node's built-in http module, before moving on to Express.js.

1Routing Without a Framework โ€” What You'll Learn

Build simple GET/POST route handling using only Node's built-in http module, before moving on to Express.js.

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

  • Reading the URL pathname
  • Checking the HTTP method
  • Handling a GET route
  • Handling a POST route
  • Returning a 404 for unmatched routes
  • Route parameters (manual parsing)
  • Query parameters (manual parsing)
  • Parsing the request body manually
  • Building a small response helper function
  • Sending proper error responses
  • Basic API versioning
2Working Example
๐Ÿ’ป Example: Routing Without a Framework
import { createServer } from "node:http";

const server = createServer((request, response) => {
  if (request.method === "GET" && request.url === "/api/courses") {
    response.writeHead(200, { "Content-Type": "application/json" });
    response.end(JSON.stringify([{ id: 1, name: "Node.js" }]));
    return;
  }

  response.writeHead(404, { "Content-Type": "application/json" });
  response.end(JSON.stringify({ message: "Route not found" }));
});

server.listen(3000);
3Best Practices & Common Pitfalls
๐Ÿ’ก Key things to remember:
  • Manually routing like this quickly becomes unwieldy โ€” this is exactly the pain point that frameworks like Express.js (Phase 7) solve.
  • Always have a fallback 404 handler at the end so unmatched routes get a proper response instead of hanging.
โ“ Frequently Asked Questions (FAQ)

Q What's the most important thing to understand about routing without a framework?

Focus on: URL Pathname Matching ยท Method Checking ยท Query Parameters ยท 404 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 routing without a framework?

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