Your Page Title
🔍

    Node.js HTTP Reference

    At the core of every web application lies the ability to handle HTTP requests and responses. In Node.js, this functionality is provided by the built-in http module—a lightweight, low-level interface that allows developers to create web servers and clients without relying on external frameworks. Whether you’re building a RESTful API, serving static files, or teaching the fundamentals of server-side programming, the http module is an essential starting point.

    Node.js is designed around an event-driven, non-blocking I/O model, and the http module exemplifies this architecture. It enables developers to respond to incoming requests asynchronously, stream data efficiently, and manage connections with minimal overhead. Unlike higher-level frameworks like Express, the http module exposes the raw mechanics of request handling, giving learners and educators a transparent view of how web servers operate under the hood.

    Creating a server with http.createServer() is straightforward. You define a callback function that receives two objects: req (the incoming request) and res (the outgoing response). These objects provide access to HTTP methods, headers, URL paths, and body data. By manipulating them, you can route requests, send responses, and implement custom logic for different endpoints. This hands-on approach is ideal for teaching core concepts like routing, status codes, content types, and asynchronous control flow.

    On the client side, the http.request() and http.get() methods allow you to make HTTP calls to external services. These are useful for consuming APIs, testing endpoints, or building proxy servers. The ability to stream data and handle events like data, end, and error makes the http module a powerful tool for building robust, real-time applications.

    For backend developers, mastering the http module means gaining fine-grained control over server behavior. You can customize headers, manage cookies, implement authentication, and optimize performance—all without the abstraction of third-party libraries. This level of control is especially valuable when building microservices, gateways, or performance-critical systems.

    Educators and technical writers benefit from the clarity and simplicity of the http module. It provides a clean, minimal interface for demonstrating how web servers work, how requests are parsed, and how responses are constructed. Learners can experiment with different HTTP methods, simulate client-server interactions, and build foundational knowledge that translates easily to more advanced frameworks.

    Recent versions of Node.js have introduced global support for fetch(), aligning server-side HTTP requests with browser-based APIs. While this simplifies client-side logic, the http module remains the backbone of server-side development, offering unmatched flexibility and performance.

    This reference guide is designed to provide a structured overview of the http module, complete with method descriptions, usage patterns, and real-world examples. Whether you’re scaffolding a tutorial, building a custom API, or teaching backend fundamentals, understanding the http module will help you write expressive, efficient, and scalable server-side code.

    Here’s a detailed and practical Node.js HTTP Reference, tailored for backend developers, educators, and learners who want to understand how to build and manage web servers using Node’s native capabilities.

    What Is the http Module in Node.js?

    The http module is a built-in Node.js module that allows you to create HTTP servers and clients. It’s the foundation for web applications, REST APIs, and real-time services built without external frameworks like Express.

    Key Methods & Properties

    Server-Side (http.createServer())

    const http = require('http');
    
    const server = http.createServer((req, res) => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end('Hello, Trish!');
    });
    
    server.listen(3000, () => {
      console.log('Server running on port 3000');
    });
    
    Method / PropertyDescription
    http.createServer()Creates an HTTP server
    server.listen(port)Starts the server on a given port
    req.methodHTTP method (GET, POST, etc.)
    req.urlRequested URL path
    req.headersIncoming request headers
    res.writeHead(statusCode, headers)Sets response status and headers
    res.write(data)Sends response body
    res.end()Ends the response

    Client-Side (http.request())

    const http = require('http');
    
    const options = {
      hostname: 'example.com',
      port: 80,
      path: '/',
      method: 'GET'
    };
    
    const req = http.request(options, (res) => {
      res.on('data', (chunk) => {
        console.log(`BODY: ${chunk}`);
      });
    });
    
    req.on('error', (e) => {
      console.error(`Problem: ${e.message}`);
    });
    
    req.end();
    
    Method / PropertyDescription
    http.request(options, callback)Makes an HTTP request
    http.get(options, callback)Shortcut for GET requests
    req.write(data)Sends request body
    req.end()Ends the request
    res.statusCodeResponse status code
    res.headersResponse headers

    Real-World Use Cases

    • REST APIs: Handle routes, parse JSON, and send responses
    • Static File Servers: Serve HTML, CSS, JS files without Express
    • Proxies & Gateways: Forward requests and manipulate headers
    • Monitoring Tools: Log requests and performance metrics

    2025 Updates & Trends

    Node.js v20+ now supports native fetch() globally, which simplifies client-side HTTP requests and aligns with browser APIs. However, the http module remains essential for server-side logic, especially when building custom frameworks or lightweight services.