Mastering Node.js HTTP Module: Build Servers, REST APIs, and Handle Requests The article explains that Node.js includes a built-in HTTP module enabling developers to create servers, handle requests and responses, and manage routing without external packages. It provides practical examples of creating a basic server, setting response headers, accessing request headers, and parsing URLs and query strings. The guide emphasizes that mastering this module is a foundational skill for backend developers building REST APIs or serving web content. Introduction to the HTTP Module Modern web applications rely heavily on communication between clients and servers. Whether you are building a REST API, handling browser requests, or serving files, understanding the HTTP module in Node.js is a foundational skill every backend developer should master. Node.js provides a built-in HTTP module that allows developers to create servers, manage requests and responses, handle routing, and even stream large amounts of data efficiently — all without installing external packages. In this article, we’ll explore the Node.js HTTP module in depth with practical examples, explanations, best practices, and real-world usage scenarios. What is HTTP? HTTP stands for: HyperText Transfer Protocol It is the protocol used for communication between: - Browsers and web servers - Frontend and backend applications - APIs and clients When you visit a website: - Your browser sends an HTTP request - The server processes it - The server sends back an HTTP response Key Features: - Create HTTP servers to handle requests and send responses - Make HTTP requests to other servers - Handle different HTTP methods GET, POST, PUT, DELETE, etc. - Work with request and response headers - Handle streaming data for large payloads Importing the HTTP Module Node.js uses the require function to import modules. js const http = require 'http' ; If you are using ES Modules: python import http from 'http'; Creating Your First HTTP Server Let’s build a simple Node.js server. js 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 on port 3000' ; } ; Understanding the Server Code http.createServer Creates an HTTP server. Parameters - req → Request object - res → Response object res.writeHead Used to set: - Status code - Headers res.writeHead 200, { 'Content-Type': 'text/plain' } ; res.end Sends the response and closes the connection. res.end 'Hello World' ; server.listen Starts the server. server.listen 3000 ; Running the Server Save the code in a file named server.js Run the server using Node.js: node server.js Visit http://localhost:3000 http://localhost:3000 in your browser to see the response. Working with HTTP Headers HTTP headers let you send additional information with your response. The res.writeHead method is used to set the status code and response headers. Setting Response Headers js const http = require 'http' ; const server = http.createServer req, res = { // Set status code and multiple headers res.writeHead 200, { 'Content-Type': 'text/html', 'X-Powered-By': 'Node.js', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Set-Cookie': 'sessionid=abc123; HttpOnly' } ; res.end '