http Module

Last Updated : Oct 2026


The http module provides a way to create an HTTP server in Node.js. It's a core module, so you don't need to install it separately.

You can use the http module to create an web services that handles incoming requests and sends responses.But There are Standard Rules for creating web services known as REST API.

REST API

REST stands for Representational State Transfer. It is an architectural style designed by Roy Fielding in 2000 to structure web services using standard HTTP protocols. For an API to be considered truly "RESTful," it follows these core concepts:

1. URLs & Resources

  • URLs are the unique identifiers for resources.
  • Each resource is identified by a unique URL(Uniform Resource Locator).
  • Resources are identified using nouns in plural form (/api/users, /api/orders), never verbs (avoid /api/getUsers or /api/createUser).

2. Stateless

  • The server does not store client state between requests.
  • Every single request from a client must contain all the information necessary for the server to understand and process it

3. HTTP Verbs

Instead of creating custom actions, REST maps basic CRUD (Create, Read, Update, Delete) operations directly to standard HTTP methods.


HTTP Methods

HTTP methods are used to specify the action to be performed on a resource.It can be idempotent or not.

Idempotency means if making the exact same request multiple times produces the same server result as making it once.

Some of the most common HTTP methods are:

  • GET: Retrieve data from a server.
  • POST: Submit data to a server to create a new resource.
  • PUT: Update data on a server to replace an existing resource.
  • PATCH: Update data on a server to modify an existing resource.
  • DELETE: Remove data from a server.

GET, PUT, and DELETE are idempotent (deleting user ID 5 once or 10 times results in user ID 5 being gone).

POST is not idempotent (calling POST /api/users 5 times creates 5 separate users).


HTTP Status Codes

HTTP status codes tell the client the result of their request. They are divided into 5 standard numeric ranges:

  • 1xx: Informational (e.g., 100 Continue, 101 Switching Protocols)
  • 2xx: Success
    • 200 OK (General success response for GET, PUT, PATCH, or DELETE.)
    • 201 Created,(Successful POST request; a new resource was successfully created.)
    • 202 Accepted,
    • 204 No Content (The request succeeded, but there is no content to send back (commonly used after DELETE).)
  • 3xx: Redirection
    • 301 Moved Permanently
    • 307 Temporary Redirect
    • 308 Permanent Redirect
  • 4xx: Client Error
    • 400 Bad Request (Malformed payload syntax, missing mandatory request body fields, or invalid formats.)
    • 401 Unauthorized (Authentication is required and has failed or hasn't been provided yet (e.g., missing JWT token).)
    • 403 Forbidden (The server understands who the user is, but the user does not have permission/roles to access the resource.)
    • 404 Not Found (The requested URI path or specific resource ID does not exist.)
    • 409 Conflict (The request conflicts with current server state (e.g., registering an email that already exists).)
  • 5xx: Server Error
    • 500 Internal Server Error (An unhandled exception or crash on the server side (e.g., uncaught code error, database down))
    • 503 Service Unavailable (Server is overloaded or down for maintenance.)

Creating an HTTP Server

Import the http module using CommonJS:

const http = require("node:http");

Import the http module using ESM:

import http from "node:http";

The http module exports a class called Server that you can use to create an HTTP server.

import { createServer } from "node:http";

let PORT = 3000;
const server = createServer((request, response) => {
  console.log(request, response);
});
server.listen(PORT, () => {
  console.log("Server running at http://localhost:" + PORT);
});

PORT is the port number that the server will listen on.It range from 0 to 65535.there is not any default port number.you can use any port number you want.Some common port numbers are:80,8080,3000,3001.

Request and Response Objects

In Above code, the request and response objects are passed to the callback function.you can use any name you want. but order is important.So let's understand the request and response objects.

Request Object

The request object contains information about the incoming request, such as the URL, HTTP method, headers, and body.Some Common properties of the request object are:

  • url: The URL of the incoming request.
  • method: The HTTP method of the incoming request (GET, POST, etc.).
  • headers: The headers of the incoming request.
  • headers: The headers of the incoming request.
  • body: The body of the incoming request.

Response Object

The response object contains information about the outgoing response, such as the status code, headers, and body.Some Common properties of the response object are:

  • statusCode: The status code of the outgoing response.
  • headers: The headers of the outgoing response.
  • body: The body of the outgoing response.
  • end: The method to end the response and send it back to the client.
  • write: The method to write data to the body of the response.
  • writeHead: The method to set the status code and headers of the response.
  • setHeader: The method to add a header to the response.So we can serve data to the client.Some Common headers are:
    • Content-Type : The content type of the response.(e.g 'text/html', 'application/json', 'text/plain')
    • Location : The location of the resource.(e.g 'http://localhost:3000')
    • Access-Control-Allow-Origin : The origin of the client.(e.g 'http://localhost:3000')
    • Access-Control-Allow-Methods : The HTTP methods that are allowed.(e.g 'GET, POST, PUT, DELETE')
    • Access-Control-Allow-Headers : The headers that are allowed.(e.g 'Content-Type, Authorization')
    • Access-Control-Allow-Credentials : Whether credentials are allowed.(e.g 'true')
    • Set-Cookie : A cookie to be sent to the client. (e.g 'session_id=123456789')

Note: We will discuss more about http,after we will learn NPM


Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR