Module 42: Web Servers with Express
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
- A server listens on a port for requests. Each request has:
- a method (GET, POST, PATCH, DELETE);
- a path (
/enquiries/E101) and maybe query parameters (?status=new); - headers (content type, cookies, authorisation);
-
maybe a body (JSON for POST and PATCH).
-
The server sends back a response with a status code, headers, and usually a body (Module 25, Lesson 4).
- Node.js can do this on its own:
import { createServer } from "node:http";
createServer((req, res) => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ message: "Hello" }));
}).listen(3001);
-
But reading paths, parsing JSON bodies and handling errors by hand is tedious. A framework like Express does that groundwork.
-
Every request is independent. HTTP doesn't remember previous requests; anything to be remembered (who's signed in, saved data) must be stored and looked up each time (Modules 44 and Level 5).
🖱️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.
