Building a Server with HTTP
Create a real web server with Node’s built-in HTTP module — and understand what Express does for you later.
What you will learn
- Create a server with the http module
- Respond to requests
- Understand ports and localhost
How a web server works
A web server is just a program that waits for visitors and replies to them. The visitor (a browser) and the server talk using HTTP (HyperText Transfer Protocol) — that name sounds scary, but it simply means the set of rules browsers and servers use to send messages back and forth on the web. Every web address you have ever typed starting with http:// uses it.
Every time someone opens a URL, this back-and-forth happens — it is called the request–response cycle:
- A browser sends a request to your server (for example, "give me the home page").
- Your server receives it as a
reqobject (what was asked, and from where). - Your code decides what to send back and writes it to the
res(response) object. - You finish the reply with
res.end(...), and the browser shows what it received. - The server keeps listening on its port, ready for the next request.
A server from scratch
Node’s built-in http module can create a web server. It listens on a port (a numbered "door" on your computer) and runs your function for every request that arrives.
// server.js
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/html" });
res.end("<h1>Hello from a Node server!</h1>");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});Walking through it: http.createServer(...) builds the server and hands it a function that runs on every request, giving you req and res. res.writeHead(200, ...) sends the status code 200 (which means "OK") plus a header saying the reply is HTML. res.end("...") sends the actual content and ends the reply. Finally server.listen(3000, ...) starts the server on port 3000 and runs the callback once it is ready.
node server.js
# Then open http://localhost:3000 in your browser
# You see: Hello from a Node server!Note: Output (in the terminal):
Server running at http://localhost:3000
The program does not exit — it stays running and listening. When you open http://localhost:3000 in a browser, the page shows Hello from a Node server! ( localhost means "this computer" and :3000 is the port). Press Ctrl+C in the terminal to stop the server.
The three pieces you keep meeting:
req— the incoming request (what the browser asked for).res— the response you send back.3000— the port the server listens on (localhost:3000).
Note: This works, but handling many URLs and methods by hand gets messy fast. That is exactly why Express exists — it makes all of this simple. You will switch to it next unit.
Q. In http.createServer((req, res) => ...), what is res?
✍️ Practice
- Build a server that responds with your name and visit it in the browser.
- Respond with different text depending on
req.url(e.g. "/" vs "/about").
🏠 Homework
- Build a server that returns a small HTML page with a heading and a list.