Node ships node:http to create HTTP servers without third-party frameworks. Every Express app ultimately sits on the same primitives—understanding them clarifies routing, headers, and status codes.
Minimal server
import http from 'node:http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello\n');
});
server.listen(3000);
Request object
req.method, req.url, and headers describe the incoming request. You must read the body from req streams for POST data—frameworks parse this for you.
Playground pattern
The runner uses a short-lived server: listen on port 0 (OS picks a free port), log, then close() and process.exit(0) so the sandbox finishes cleanly.
Important interview questions and answers
- Q: What does res.writeHead do?
A: Sends status code and response headers before the body—must happen beforeres.endfor that response. - Q: HTTP vs HTTPS in Node?
A:node:httpsmodule with TLS certificates—often terminated at a load balancer in production.
Self-check
- Which module creates a bare HTTP server?
- Why close the server in playground demos?
Challenge
Hello HTTP
- Run the default
node:httpserver code. - Confirm the terminal logs a port and "Server closed".
- Change the response body to include your name.
Done when: output shows your custom greeting and a clean shutdown.
Interview prep
- createServer vs Express?
http.createServeris low-level—you parse URLs and bodies yourself; Express adds routing, middleware, and helpers on top of the same HTTP module.