Module 42: Web Servers with Express

Lesson 1 of 5 · 15–25 minutes

HTTP from the server's side

🏠The everyday picture

In Modules 6 and 25 you were the customer placing orders. Now you're behind the counter: reading each order slip, deciding what to do, and handing back the result with a receipt number.

📘What it really is

import { createServer } from "node:http";

createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "application/json" });
  res.end(JSON.stringify({ message: "Hello" }));
}).listen(3001);

🖱️Try it

Run the plain Node.js server above as src/plain.ts. Visit http://localhost:3001 in your browser, and open the Network tab to see the status, headers and body. Then try curl -i http://localhost:3001 in the terminal.

✅Quick check

What four parts can an HTTP request have?

Show the answer

A method, a path (with optional query parameters), headers, and sometimes a body.

Does the server remember who sent the previous request?

Show the answer

Not by itself. HTTP is stateless; anything remembered must be stored and looked up each time.

← BackModule 42
ProLearners Assist