Node.js HTTP Server in JavaScript

Introduction

Node’s built-in http module creates HTTP servers without external frameworks. Understanding raw request/response handling clarifies how Express, Fastify, and Next.js build on the same primitives. This chapter serves a simple API and parses URLs.

Prerequisites

Minimal Server

javascript
// server.mjs
import http from "node:http";
 
const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
  res.end("Hello from Node\n");
});
 
const PORT = 3000;
server.listen(PORT, () => {
  console.log(`http://127.0.0.1:${PORT}`);
});

Run node server.mjs and visit the URL in a browser or curl.

Routing by URL

Read Request Method and Body (POST)

Frameworks handle streaming and size limits for you.

JSON API Response Helper

javascript
function sendJson(res, status, data) {
  res.writeHead(status, { "Content-Type": "application/json" });
  res.end(JSON.stringify(data));
}

When to Use a Framework

Raw http is great for learning. Production APIs often use Express, Fastify, or Hono for routing, middleware, and security headers.

Mini Example: In-Memory Todos API

Test: curl http://127.0.0.1:3000/todos

FAQ

HTTP vs HTTPS?

https module adds TLS certificates—terminate TLS at a reverse proxy (nginx) in many deployments.

Node fetch?

Available globally in current Node LTS for outbound requests; http is for inbound servers.

WebSockets?

Separate ws library or http.Server upgrade—beyond this intro.

What comes next?

Async and reliability in Node.