The http
module in Node.js is a powerful built-in utility that serves as the foundation for web server development. It allows developers to create servers, manage HTTP requests and responses, and build dynamic web applications, without relying on external libraries. Leveraging Node.js’s non-blocking I/O and event-driven architecture, the http
module ensures efficient performance even under heavy load.
With just a few lines of code, developers can spin up a server that listens to client requests and responds with content ranging from plain text to complex JSON. The module exposes objects like http.IncomingMessage
and http.ServerResponse
, giving precise control over headers, status codes, and body formatting.
Whether you’re building a simple “Hello, World!” server or a robust API backend, the http
module provides granular access to the request lifecycle. It supports standard HTTP methods, GET, POST, PUT, DELETE allowing developers to handle routing manually or integrate with frameworks like Express for streamlined development.
Understanding the http
module is a key stepping stone in mastering backend development with Node.js. It not only strengthens your grasp of server architecture but also prepares you to handle real-world scenarios like authentication, data parsing, and RESTful API design.
Features-
1. Server Creation– Enables creation of HTTP servers using http.createServer()
. Allows listening for and handling client requests directly
2. Request Handling– Provides access to request details such as URL, method, headers, and query parameters. Supports parsing and routing based on request data
3. Response Management– Lets you send HTTP responses with custom headers and status codes. Supports streaming and output of text, JSON, HTML, etc.
4. HTTP Method Support– Handles all major HTTP verbs: GET, POST, PUT, DELETE, etc. Useful for building RESTful endpoints manually
5. Streaming Capability– Efficiently processes large payloads using native stream interfaces. Ideal for serving files or real-time data
6. Event-Driven Architecture– Uses event listeners (request
, connection
, close
) for asynchronous control. Supports reactive server logic without blocking the main thread
7. Header Manipulation– Allows reading and setting custom headers for both requests and responses. Enables implementation of cookies, content negotiation, and caching strategies.
8. HTTPS Compatibility– Integrates seamlessly with the https
module for SSL/TLS encryption. Supports secure client-server communication using certificates.
Example Code-
const http = require(‘http’);
const server = http.createServer((req, res) => {
res.writeHead(200, { ‘Content-Type’: ‘text/plain’ });
res.end(‘Hello, World!’);
});
server.listen(3000, () => {
console.log(‘Server running at http://localhost:3000/’);
});
Some Methods-
CLASS | METHOD | DESCRIPTION |
http | createServer() | Creates a new HTTP server instance |
server | listen(port, [callback]) | Starts the server and listens on the specified port |
server | close([callback]) | Stops the server from accepting new connections |
ServerResponse | writeHead(statusCode, headers) | Sets response status code and headers |
ServerResponse | write(data) | Sends a chunk of the response body |
ServerResponse | end([data]) | Ends the response and optionally sends final data |
IncomingMessage | setTimeout(timeout, callback) | Sets a timeout for the request |
IncomeMessage | on(event, listener) | Attaches event listeners like 'data' , 'end' , 'error' |