Index - Node documentation

Usage

import * as mod from "node:http";

To use the HTTP server and client one must require('node:http').

The HTTP interfaces in Node.js are designed to support many features of the protocol which have been traditionally difficult to use. In particular, large, possibly chunk-encoded, messages. The interface is careful to never buffer entire requests or responses, so the user is able to stream data.

HTTP message headers are represented by an object like this:

{ "content-length": "123",
  "content-type": "text/plain",
  "connection": "keep-alive",
  "host": "example.com",
  "accept": "*" }

Keys are lowercased. Values are not modified.

In order to support the full spectrum of possible HTTP applications, the Node.js HTTP API is very low-level. It deals with stream handling and message parsing only. It parses a message into headers and body but it does not parse the actual headers or the body.

See message.headers for details on how duplicate headers are handled.

The raw headers as they were received are retained in the rawHeadersproperty, which is an array of [key, value, key2, value2, ...]. For example, the previous message header object might have a rawHeaderslist like the following:

[ 'ConTent-Length', '123456',
  'content-LENGTH', '123',
  'content-type', 'text/plain',
  'CONNECTION', 'keep-alive',
  'Host', 'example.com',
  'accepT', '*' ]

Classes

c
Agent

An Agent is responsible for managing connection persistence and reuse for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty, at which time the socket is either destroyed or put into a pool where it is kept to be used again for requests to the same host and port. Whether it is destroyed or pooled depends on thekeepAlive option.

c
ClientRequest

This object is created internally and returned from request. It represents an in-progress request whose header has already been queued. The header is still mutable using the setHeader(name, value),getHeader(name), removeHeader(name) API. The actual header will be sent along with the first data chunk or when calling request.end().

c
IncomingMessage

An IncomingMessage object is created by Server or ClientRequest and passed as the first argument to the 'request' and 'response' event respectively. It may be used to access response status, headers, and data.

c
OutgoingMessage

This class serves as the parent class of ClientRequest and ServerResponse. It is an abstract outgoing message from the perspective of the participants of an HTTP transaction.

c
Server
No documentation available
c
ServerResponse

This object is created internally by an HTTP server, not by the user. It is passed as the second parameter to the 'request' event.

Functions

f
createServer

Returns a new instance of Server.

f
get

Since most requests are GET requests without bodies, Node.js provides this convenience method. The only difference between this method and request is that it sets the method to GET by default and calls req.end()automatically. The callback must take care to consume the response data for reasons stated in ClientRequest section.

f
request

options in socket.connect() are also supported.

f
setMaxIdleHTTPParsers

Set the maximum number of idle HTTP parsers.

f
validateHeaderName

Performs the low-level validations on the provided name that are done whenres.setHeader(name, value) is called.

f
validateHeaderValue

Performs the low-level validations on the provided value that are done whenres.setHeader(name, value) is called.

Interfaces

I
AgentOptions
No documentation available
I
ClientRequestArgs
No documentation available
I
IncomingHttpHeaders
No documentation available
I
InformationEvent
No documentation available
I
OutgoingHttpHeaders
No documentation available
I
RequestOptions
No documentation available
I
ServerOptions
No documentation available

Type Aliases

T
OutgoingHttpHeader
No documentation available
T
RequestListener
No documentation available

Variables

v
globalAgent
No documentation available
v
maxHeaderSize

Read-only property specifying the maximum allowed size of HTTP headers in bytes. Defaults to 16KB. Configurable using the --max-http-header-size CLI option.

v
METHODS
No documentation available
v
STATUS_CODES
No documentation available