Skip to content

Minimal HTTP Server With Routing

Start a Node.js HTTP server with no framework: exact-path routes for / and /health, and a 404 for everything else.

Language: Node.js

Added on: 21st August 2026

import http from "node:http";

http.createServer((req, res) => {
  if (req.url === "/") {
    return res.end("Home");
  }
  if (req.url === "/health") {
    return res.end("OK");
  }
  res.writeHead(404);
  res.end("Not Found");
}).listen(3000);

Caveats

ES module: run as .mjs, or set type module in package.json. Listens on port 3000. req.url includes the query string, so /health?ready=1 is 404.