Loading video player…

Follow a Request Through the Server

In this lesson, you followed requests into the server from the last lesson and logged what Node.js hands the request listener each time.

Key Points

  • node --watch server.ts restarts the server every time you save. A running server keeps the code it started with, so watch mode saves you from stopping and starting it by hand.
  • Only one program can listen on a port at a time. If an old copy of the server is still running, a new one fails with listen EADDRINUSE: address already in use. Stop the old copy with Ctrl+C (and the failed watch command too), then run node --watch server.ts in one terminal. This time it prints the listening message.
  • Node.js calls your listener once for every request. Three curl requests meant three calls and three log lines. In the browser used here, one visit meant two: the page and /favicon.ico, the icon on the browser’s tab. Another browser might not ask for the icon.
  • The listener doesn’t run when the server starts, the way the rest of your file does. The listening message printed once per start, but the code inside the listener runs again for every request.
  • req.method and req.url describe this one request. For requests like the ones here, req.url holds only the path and the query string, such as /plots?vacant=true, with no host name or port.
  • req.headers is an object with lowercase header names, whatever the client sent. curl sent Host, User-Agent and Accept. Only log every header on your own machine, because headers can carry passwords. Once you’ve had your look, delete that line and save.
  • Between requests, the program waits. The listening server keeps it alive, and the event loop sits idle until the next request arrives.

So the rule is that each request means one call to your listener, and req holds that request.

The Final Code

Here’s server.ts after you’ve deleted the headers line, with one log line left in the listener:

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

const PORT = 3000;

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

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

Started with node --watch server.ts and asked for plot 7 with curl -i http://localhost:3000/plots/7, it logs this line in the server’s terminal:

Language: Shell
GET /plots/7

Your server still answers every request with the same plain text. In the next lesson, Send JSON With the Right Status, you’ll decide what goes back to the client.

00:00 Your server from the last lesson answers every request with the same greeting. Start it with node server.ts, as before, and ask it for the home page.

00:09 You get the greeting. Ask it for plot 7, and you get the greeting again. To answer Mrs. Okafor’s questions, your server has to tell those requests apart. So first, you’ll watch what Node hands your listener each time a request arrives.

00:28 You’ll change this file a few times, and a running server keeps the code it started with. So instead of restarting it by hand, use Node’s watch option. Every time you save, Node restarts the server for you.

00:42 Your first server is still running. See what happens if you start the watch command in your second terminal. It fails with the error code EADDRINUSE: address already in use.

00:56 Only one program can listen on port 3000 at a time, and your first server got there first. Press Ctrl+C in both terminals, then start the watch command in the first one.

01:08 This time, it prints the listening message.

01:12 Now add one line at the top of the listener.

01:17 It logs the request’s method and its URL, two properties of the req object. Save the file. Node notices the change, restarts the server, and prints the listening message again.

01:31 Send a few requests from your second terminal. Ask for the home page, and a line appears: GET /. Ask for plot 7, and the next line shows GET and the plot’s path.

01:45 Send a POST to the waiting list with curl’s -X option, and a third line appears.

01:51 Each request travels into the same function. Node calls your listener once per request, and each time, req holds that one request’s details.

02:02 Mrs. Okafor’s most common call asks which plots are free. The finished board answered it with a query string, vacant=true. Your turn: pause the video and predict the URL your listener logs for this request.

02:22 It logs /plots?vacant=true.

02:28 For requests like these, the URL in req holds only the path and the query string. The host name and the port aren’t part of it.

02:38 A browser is a client too. Open the address for plot 7 in one, and the page shows your greeting. With this browser, your server’s terminal shows two new lines.

02:49 The second asks for favicon.ico, the small icon on the browser’s tab. Another browser might not ask for it. Here, one visit meant two requests, so Node called your listener twice.

03:02 Watch out here: the listener doesn’t run when the server starts, the way the rest of your file does. The listening message printed once per start, but the code inside the listener runs again for every request.

03:17 Headers arrive with every request too. Add a second line to the listener that logs the request’s headers, and save. Ask for plot 7 with curl, and the headers come out as an object with the host, the user agent curl chose to send, and what curl accepts back.

03:35 Curl sent those names with capital letters. Node gives you every header name in lowercase, whatever the client sent. Only log every header on your own machine, because headers can carry passwords.

03:48 Once you’ve had your look, delete that line and save.

03:54 Between requests, your program waits. The listening server keeps it alive, and the event loop sits idle until the next request arrives. Then Node calls your listener, and the loop goes back to waiting.

04:08 So the rule is that each request means one call to your listener, and req holds that request. Your server now sees each request’s method, URL and headers.

04:21 However, it still answers every one with the same plain text. In the next lesson, Send JSON With the Right Status, you’ll decide what goes back to the client.

Become a Member to join the conversation.