Start Your Own Server

In the last lesson, you called the finished plot board with curl -i and fetch() and watched it choose the status code and body for each response. You treated it as a black box. Now you’ll open the box and write the kind of program that sits inside it.

The program you write here is small. It answers every request with the same line of text, whatever its method or path. However, it’s a real server. You’ll start it from your own terminal, call it the same way you called the board, and stop it when you’re done. Start by setting up the project that you’ll build the plot board in for the rest of the course.

Open the Starting Project

Your server needs a folder to live in, and the course’s starting project gives you one that’s already set up. It’s the 02-start-server/starting-project folder of the course’s code download, the ZIP file you extracted in the last lesson. If you need the download again, you’ll find its link on the course’s page and in the first lesson, Your First Server With node:http (Overview).

The starting project holds three files of its own, plus the package-lock.json that npm keeps. Its package.json sets "type": "module" and defines a check script. Its tsconfig.json turns on the strict TypeScript settings you’ve used before. Its plots.json holds Hollybank’s twenty-four plots, which your server will read in a later lesson.

Open a terminal in the starting-project folder and install the project’s development dependencies with npm:

Language: Shell
$ npm install

added 4 packages, and audited 5 packages in 1s

found 0 vulnerabilities

The packages are TypeScript itself and Node.js’s type definitions, @types/node. They’re only there for npm run check. The server you write imports nothing but Node.js’s own modules, so it needs no packages to run. The numbers and the time in your output may differ.

Write a Server That Answers Every Request

The scripts you’ve written so far do their work and exit. A server keeps running instead, so it can wait for requests and answer each one as it arrives. Node.js’s built-in node:http module gives you that.

Create a file called server.ts in the project’s folder, next to package.json, with this code:

Language: TypeScript Filename: server.ts
import { createServer } from "node:http";

const PORT = 3000;

const server = createServer((req, res) => {
  res.end("Hello from the Hollybank plot board!\n");
});

server.listen(PORT, () => {
  console.log(`Listening on http://localhost:${PORT}`);
});

The arrow function you pass to createServer() is the request listener. Node.js calls it once for every request that reaches the server, whatever its method or path. Each time, Node.js hands it two objects.

The first, req, is the incoming request. The second, res, is the response you’re building. This listener ignores req for now and calls res.end() with one line of text. That sends the text as the response’s body and finishes the response.

The call to server.listen() starts the server. It accepts connections on port 3000, the number that picks out this program among the programs on your computer that take network connections. The callback runs once the server is ready, so the message prints only when requests can get through.

The name localhost in that message is only the name you use to reach the server. When you call listen() with a port and no host, the server accepts connections from every network your computer is on. Another device on the same Wi-Fi can reach it unless a firewall stops it. Passing a host, as in server.listen(PORT, "127.0.0.1"), makes the server accessible only from your computer.

Before you run anything, check the file with the project’s check script, which runs the TypeScript compiler without writing any files:

Language: Shell
$ npm run check

> [email protected] check
> tsc --noEmit

No output after the tsc --noEmit line means no type errors. Your server is ready to start.

Start It, Call It, and Stop It

A server is useful only while it’s running, so start yours and send it a few requests. If the finished board from the last lesson is still running, stop it first with Ctrl+C, because both programs use port 3000.

Start your server with Node.js from the project’s folder:

Language: Shell
$ node server.ts
Listening on http://localhost:3000

The prompt doesn’t come back, just as it didn’t with the finished board. The listening server keeps your program alive, waiting for requests. Leave this terminal alone and open a second terminal for your requests. Make sure it’s in the same project folder, next to server.ts and package.json, because you’ll run a script from there in a moment.

Ask your server for its home page with curl -i:

Language: Shell
$ curl -i http://localhost:3000/
HTTP/1.1 200 OK
Date: Thu, 26 Nov 2026 14:22:29 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 37

Hello from the Hollybank plot board!

Your code wrote only the body. Node.js filled in the rest: a 200 OK status line, a Date header, and a Content-Length header that counts the body’s 37 bytes. There’s no Content-Type header, though, so the client has to guess what the body is. You’ll choose the status and headers, including Content-Type, yourself later in the course.

The finished board answered /plots/7 with Daniel Price’s plot. Before you run the next command, decide what your server will send back for the same path:

Language: Shell
$ curl -i http://localhost:3000/plots/7
Language: Shell
$ curl -i http://localhost:3000/plots/7
HTTP/1.1 200 OK
Date: Thu, 26 Nov 2026 14:22:29 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Content-Length: 37

Hello from the Hollybank plot board!

You get the same greeting. Your listener never looks at req, so it can’t tell /plots/7 from / or any other path.

Every request runs the same function and gets the same answer, whatever its method, so a POST would get the greeting too. The one difference is a HEAD request, which asks for the response without its body. It gets the 200 OK status line and headers, but no greeting.

A script gets the same answer, too. Save this as call-server.ts in the project’s folder:

Language: TypeScript Filename: call-server.ts
const url = "http://localhost:3000/plots/7";
const response = await fetch(url);
console.log(response.status, response.statusText);
console.log(await response.text());

Run it in your second terminal while the server is still running:

Language: Shell
$ node call-server.ts
200 OK
Hello from the Hollybank plot board!

When you’re done, go back to the server’s terminal and press Ctrl+C. The prompt comes back because the program has stopped. Run the first curl command again, and you’ll see what a client gets when nothing is listening:

Language: Shell
$ curl -i http://localhost:3000/
curl: (7) Failed to connect to localhost port 3000 after 0 ms: Couldn't connect to server

There’s no status code at all this time. With no program on port 3000, there’s no server to send a response, so curl reports that the connection failed. The time in your message may differ.

Before you move on, check that you can run a server and explain what it did:

  1. When does Node.js call the function you pass to createServer()?
  2. Why didn’t the prompt come back after node server.ts, and how did you get it back?
  3. Your server listens with server.listen(3000). Can a phone on the same Wi-Fi reach it, and what would make it reachable only from your computer?
  1. Node.js calls it once for every request that reaches the server, whatever the request’s method or path.
  2. The listening server keeps the program running, waiting for requests. Pressing Ctrl+C stopped the program, and the prompt came back.
  3. It can, unless a firewall stops it, because listen() with no host accepts connections from every network your computer is on. Passing the host, as in server.listen(3000, "127.0.0.1"), makes the server accessible only from your computer.

Your server answers every request, but it can’t yet tell one request from another. In the next lesson, Follow a Request Through the Server, you’ll watch what Node.js hands your listener each time a request arrives.

Become a Member to join the conversation.