R
Rishtaara
Node.js Fundamentals
Lesson 3 of 8Article14 min

Building HTTP Server with Core Node

Before Express, learn how raw request and response objects work. This helps you debug headers, status codes, and response lifecycle in any framework.

Using the built-in http module

Before Express, learn how raw request and response objects work. This helps you debug headers, status codes, and response lifecycle in any framework.

Minimal HTTP server
import http from "node:http";

const server = http.createServer((req, res) => {
  if (req.url === "/health") {
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ ok: true }));
    return;
  }

  res.writeHead(404, { "Content-Type": "text/plain" });
  res.end("Not found");
});

server.listen(3000, () => {
  console.log("Server running on http://localhost:3000");
});

HTTP request lifecycle basics

  • Client sends method, URL, headers, and optional body.
  • Server routes to handler logic.
  • Handler validates input and performs business work.
  • Response returns status code, headers, and payload.