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

Node.js · Class

IncomingMessage

http.IncomingMessage is the readable-stream object Node.js provides for an incoming HTTP request or client response. It exposes HTTP metadata and carries the message body.

How it works

IncomingMessage is the readable stream Node.js supplies for an incoming HTTP request or response. It exposes request metadata such as method, URL, headers, and socket information, while also providing response metadata when it represents a client response.

Syntax

class http.IncomingMessage extends stream.Readable

Returns

An instance supplied by Node.js to HTTP server request and client response events; it is a readable stream carrying the message payload.

Key points

  • IncomingMessage is a readable stream class, allowing HTTP payload data to be consumed with stream APIs.
  • Node.js creates it for incoming server requests and for responses received by client requests.
  • A server receives it through the request event, while an HTTP client receives it through the response event.
  • The object provides access to HTTP status information, headers, and payload data.
  • The complete property can be used to determine whether the full HTTP message has arrived.
  • rawHeaders preserves the received header sequence and spelling, unlike the normalized headers view.

Common mistakes

  • Do not assume the message was fully received merely because the object exists; use complete when checking message completion.
  • The IncomingMessage object is separate from its socket, and the socket can be reused for keep-alive connections.

Example: Read an incoming HTTP request

import http from 'node:http';

const server = http.createServer(async (request, response) => {
  const chunks = [];
  for await (const chunk of request) chunks.push(chunk);

  response.setHeader('content-type', 'application/json');
  response.end(JSON.stringify({
    method: request.method,
    url: request.url,
    body: Buffer.concat(chunks).toString()
  }));
});

server.listen(3000);

Creates an HTTP server that consumes the IncomingMessage request stream and returns selected request metadata and body.

When to use it

Read request metadata and body

HTTP server request handling

Consume an HTTP response stream

HTTP client response handling

Inspect original headers and completion state

Protocol diagnostics

Interview questions

QWhat is IncomingMessage, and how is it used?

IncomingMessage is created by an HTTP server or client request and passed to the request or response event. It extends stream.Readable, so request or response data is consumed as a stream.

Follow-up: How would you process a large request body? Consume the readable incrementally, avoiding accumulation of the entire body in memory.

QWhich metadata does IncomingMessage expose?

For server requests, method and url describe the HTTP request, while headers provides the parsed request headers. For client responses, statusCode and statusMessage expose the response status information.

Follow-up: Why should header handling account for repeated fields? Node.js may represent some header values as arrays, so code should not assume every value is a string.

QHow can code determine message completion and inspect original headers?

The complete property indicates whether the complete HTTP message has been received. The rawHeaders property preserves the headers as they were received, rather than presenting only the normalized headers object.

Follow-up: When might rawHeaders matter? It is useful when preserving original header spelling or repeated header entries during diagnostics or protocol-sensitive processing.

  • http.Server
  • http.ClientRequest
  • stream.Readable
  • http.ServerResponse
  • message.complete
  • message.headers
  • message.rawHeaders

Source: http.IncomingMessage | Node.js 26.10.0 Documentation. Checked 2026-10-15.