The http module allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP). It is the foundation for web servers in Node.js—every framework you will use later (Express, Fastify, Koa) is built on top of this module.Understanding the raw http module is like learning to drive a manual transmission before switching to automatic. You will rarely use it directly in production, but knowing how it works gives you the ability to debug framework-level issues, understand what middleware is actually doing under the hood, and make informed decisions about when a framework is helping versus getting in your way.
The http.createServer() method includes a request listener function which is automatically added to the 'request' event.
const server = http.createServer((req, res) => { // This callback fires for EVERY incoming HTTP request. // Node.js does not create a new thread per request--this single function // handles all requests via the event loop, which is why Node.js can handle // thousands of concurrent connections with minimal overhead. res.write('Hello World'); res.end(); // Always call res.end() -- forgetting this leaves the connection hanging});server.listen(5000, () => { console.log('Server running on port 5000...');});
Common pitfall — forgetting res.end(): If you never call res.end(), the client will hang indefinitely waiting for a response, and the connection stays open consuming resources. In a production server, this leads to connection pool exhaustion where your server appears frozen even though it is actually running.
Run this script and navigate to http://localhost:5000 in your browser.
const server = http.createServer((req, res) => { if (req.url === '/') { res.write('Welcome to the Home Page'); res.end(); } else if (req.url === '/about') { res.write('Welcome to the About Page'); res.end(); } else { res.statusCode = 404; res.write('Page Not Found'); res.end(); }});
Unlike GET requests, POST data comes in chunks and must be collected. This is because HTTP request bodies can be arbitrarily large (file uploads, for example), so Node.js streams the data to you piece by piece rather than buffering the entire body into memory.
const server = http.createServer((req, res) => { if (req.method === 'POST' && req.url === '/api/users') { let body = ''; // Each 'data' event delivers a chunk of the request body. // For a small JSON payload, you might get it all in one chunk. // For a large file upload, you could get hundreds of chunks. req.on('data', chunk => { body += chunk.toString(); // SECURITY: Limit body size to prevent memory exhaustion attacks. // Without this check, an attacker can send a 10GB payload and crash your server. if (body.length > 1e6) { // ~1MB limit res.writeHead(413, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Payload too large' })); req.destroy(); } }); // 'end' fires when the entire body has been received req.on('end', () => { try { const user = JSON.parse(body); console.log('Received user:', user); res.writeHead(201, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ message: 'User created', user })); } catch (error) { res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Invalid JSON' })); } }); }});
The http module creates web servers without external dependencies
Use req.url and req.method for routing—but this gets unwieldy fast, which is why frameworks like Express exist
POST data arrives in chunks via 'data' and 'end' events—always limit body size to prevent memory exhaustion attacks
Parse query strings with the URL class (not the deprecated url.parse())
Set appropriate Content-Type headers for responses—mismatched headers cause subtle client-side bugs
Always call res.end() to avoid hanging connections
Use https module with SSL certificates for production—in practice, most teams terminate TLS at a reverse proxy (Nginx, load balancer) rather than in Node.js directly
Frameworks like Express.js abstract this complexity while still using http under the hood