// server.mjs — a tiny web server using only Node's built-in http module. // Run it with: node server.mjs then open http://localhost:3000 import { createServer } from "node:http"; const PORT = 3000; const server = createServer((request, response) => { // Every request the browser makes shows up here. console.log(`${request.method} ${request.url}`); if (request.url === "/") { response.writeHead(200, { "Content-Type": "text/html; charset=utf-8" }); response.end("
The browser asked, the server answered.
"); return; } // Anything else: tell the browser we don't have it. response.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" }); response.end("404 Not Found"); }); server.listen(PORT, () => { console.log(`Listening on http://localhost:${PORT}`); });