Node.js lesson · 38 min

Node.js HTTP Server

Create a small HTTP server with the built-in `node:http` module and test it with curl.

Code runs locally in your browser

What you will practice

  • Create a server with `node:http`.
  • Return plain text and JSON responses.
  • Understand request URL, method, status code, and headers.

What this means

A Node.js server is a long-running process that listens for HTTP requests.

The built-in `node:http` module can create a server without Express or any third-party framework.

Every request has a method and URL. Every response should have a status code, headers, and a body.

If this is your first time seeing this

A Node.js HTTP server is a JavaScript program that keeps running and waits for requests.

Start with the built-in `node:http` module before jumping to frameworks.

Mini glossary

HTTP server
A program that listens for HTTP requests and sends HTTP responses.
Request
Incoming data from a client, including method and URL.
Response
The status code, headers, and body sent back to the client.

Example from everyday life

An HTTP server is like a small reception desk. A visitor asks for `/health` or `/api/tools`, and the desk chooses the correct answer, puts the right label on it, and sends it back.

How it works step by step

  1. Create `server.js` and import `createServer` from `node:http`.
  2. Inside the request handler, inspect `req.url` and `req.method`.
  3. Set `statusCode` and `Content-Type` before ending the response.
  4. Run `node server.js`, then test the server from another terminal with `curl`.

Where you will use this

  • A health endpoint returns `ok` for uptime checks.
  • A JSON API returns tool data to a frontend.
  • A webhook endpoint receives events from another service.

Before you run the code

This lesson intentionally starts with built-in Node.js. Frameworks such as Express are easier to appreciate after you see the raw request/response model.

Stop the server with `Ctrl+C` when you are done.

Run it locally

These examples are meant for a local Node.js project. Create the files shown below, then run the terminal commands from the project folder.

Create the project

mkdir lumio-http-demo
cd lumio-http-demo
touch server.js

Pure Node.js HTTP server

server.js
const { createServer } = require("node:http");

const hostname = "127.0.0.1";
const port = Number(process.env.PORT || 3000);

const tools = [
  { id: "json-formatter", title: "JSON Formatter" },
  { id: "http-status-code-reference", title: "HTTP Status Code Reference" }
];

const server = createServer((req, res) => {
  if (req.method === "GET" && req.url === "/health") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "text/plain; charset=utf-8");
    res.end("ok");
    return;
  }

  if (req.method === "GET" && req.url === "/api/tools") {
    res.statusCode = 200;
    res.setHeader("Content-Type", "application/json; charset=utf-8");
    res.end(JSON.stringify({ tools }));
    return;
  }

  res.statusCode = 404;
  res.setHeader("Content-Type", "application/json; charset=utf-8");
  res.end(JSON.stringify({ error: "Not found" }));
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Start the server

node server.js

Test from another terminal

curl http://127.0.0.1:3000/health
curl http://127.0.0.1:3000/api/tools
curl -i http://127.0.0.1:3000/missing

Common beginner mistakes

  • Forgetting that the server keeps running until you stop it with Ctrl+C.
  • Trying to use the same port in two terminal windows at once.
  • Returning JSON without setting a JSON Content-Type header.

Why there is no browser runner here

This lesson uses real Node.js APIs and terminal commands. Run the examples locally with Node.js so you learn the same workflow used in actual backend projects.

Try changing this next

  • Add a `/` route that returns `Welcome to Lumio`.
  • Change the server port with `PORT=4000 node server.js`.
  • Add one more tool to the `tools` array and call `/api/tools` again.