Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

http

Node.js's node:http module provides low-level HTTP client and server APIs. It streams request and response data instead of buffering complete messages, leaving application-level parsing and routing to the caller.

How it works

The node:http module exposes low-level client and server APIs. It parses HTTP messages into headers and bodies while using streams rather than buffering complete requests or responses, so applications handle routing, body interpretation, and other higher-level behavior themselves.

Syntax

import * as http from 'node:http';
const server = http.createServer(requestListener);
const request = http.request(options[, callback]);
const getRequest = http.get(options[, callback]);

Parameters

NameTypeDescription
requestListenerFunctionThe request listener invoked for incoming requests; it receives an IncomingMessage and ServerResponse.

Returns

The module namespace provides HTTP client and server APIs; http.createServer() returns an HTTP server, while http.get() returns an http.ClientRequest.

Key points

  • The module supports both making HTTP client requests and creating HTTP servers.
  • HTTP payloads are exposed through streaming interfaces, which supports large and chunked messages.
  • The module supplies protocol primitives rather than a complete web framework.
  • Its core responsibilities are moving HTTP data through streams and separating messages into their main parts.
  • Use the node:http built-in specifier with either CommonJS or ES module syntax.

Common mistakes

  • The module does not interpret header values or request bodies; application code must perform that parsing.

Example: Minimal HTTP server

import * as http from 'node:http';

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!\n');
});

server.listen(3000, () => {
  console.log('Listening on http://localhost:3000');
});

Creates a basic HTTP server, writes a response status and header, then ends each response.

When to use it

Build a basic HTTP server

Expose a lightweight HTTP endpoint without adding a web framework.

Implement an HTTP client

Send outbound HTTP requests to another service with explicit request control.

Stream request and response bodies

Process large or chunked payloads incrementally.

Build backend infrastructure

Create framework or infrastructure primitives around Node's protocol-level APIs.

Interview questions

QWhat does http.createServer() provide?

http.createServer() creates an HTTP server and accepts a request listener. The listener receives the incoming request and server response objects, allowing the application to inspect the request and write the response.

Follow-up: Why is the HTTP API described as low-level? It handles stream processing and message parsing, but leaves application-level interpretation and routing to the user.

QHow does the module support large HTTP messages?

Node's HTTP interface avoids buffering an entire request or response, allowing large or chunked messages to be streamed. This reduces the need to hold complete payloads in memory.

Follow-up: What trade-off follows from streaming? Handlers must correctly consume and produce stream data rather than assuming the complete body is immediately available.

QHow does http.get() differ from http.request()?

http.get() is a convenience method for common GET requests. It sets the method to GET and invokes req.end() automatically, while http.request() is used when more control is needed.

Follow-up: When should http.request() be preferred? Use it when the method, request body, headers, or request lifecycle need explicit control.

  • http.createServer
  • http.request
  • http.get
  • http.Server
  • http.IncomingMessage
  • http.ServerResponse
  • http.Agent

Source: HTTP. Checked 2026-10-15.