AceDevHub
Beginner Node.js Interview QuestionsBeginnerConcept

Node.js · Question 26

What is the Node.js http module?

Direct answer

The http module creates HTTP servers and clients on top of TCP — http.createServer handles requests with a callback or request listener; production APIs often use Fastify or Express built on the same Node HTTP foundation.

Node's low-level http module maps directly to HTTP/1.1 semantics — method, headers, status code, body stream — without routing or JSON parsing. Frameworks add those ergonomics.

  • http.createServer — returns http.Server; emits 'request' for each incoming connection.
  • http.request / fetch — client-side outbound calls to other APIs.
  • https module — TLS layer; terminate SSL at Caddy/reverse proxy in AceDevHub prod.
  • HTTP/2 — http2 module for multiplexing; less common in simple REST APIs.
http-basics.mjs
import http from "node:http";

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

server.listen(4000);