module http

Supported only in the v8 engine. Introduced in Max v9.2.0.

The http module exposed by require("http") to access HTTP/1.1 clients and servers.

This is a minimal http subset, not full Node parity.

Example

const http = require("http");
const server = http.createServer((req, res) => {
  res.writeHead(200, { "content-type": "text/plain" });
  res.end("hello http\n");
});
server.listen(8090, "127.0.0.1");

Index

Properties

Functions

Classes

Interfaces

Type Aliases

class Agent

A keep-alive connection pool for client requests.

Constructors

Methods

new Agent(options)

new Agent(options?: { keepAlive?: boolean; keepAliveMsecs?: number });

Constructs a new instance of the Agent class

ParameterTypeDescription
optional optionsobjectoptional options.keepAlive boolean
optional options.keepAliveMsecs number

destroy()

Closes pooled connections.

destroy(): void;

class ClientRequest

An outgoing client request.

Headers become immutable after the first body write or end(). Events: response, drain, close, error, timeout.

Methods

abort()

Aborts the request.

abort(): void;

destroy(error)

Destroys the request, optionally with an error.

destroy(error?: Error): void;
NameTypeDescription
optional errorError

end(chunk, encoding, callback)

Writes an optional final chunk and finishes the request.

end(
  chunk?: string | buffer.IOBuffer | ArrayBufferView,
  encoding?: string,
  callback?: () => void,
): void;
NameTypeDescription
optional chunkstring | buffer.IOBuffer | ArrayBufferView
optional encodingstring
optional callback() => void

getHeader(name)

Returns a previously set header value.

getHeader(name: string): string | string[] | undefined;
NameTypeDescription
namestring
Return Valuestring | string[] | undefined

off(event, listener)

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

removeHeader(name)

Removes a previously set header.

removeHeader(name: string): void;
NameTypeDescription
namestring

setHeader(name, value)

Sets a request header. Array values emit repeated header lines.

setHeader(name: string, value: string | string[]): void;
NameTypeDescription
namestring
valuestring | string[]

setTimeout(timeout, callback)

Sets a per-request timeout.

setTimeout(timeout: number, callback?: () => void): this;
NameTypeDescription
timeoutnumber
optional callback() => void
Return Valuethis

write(chunk, encoding, callback)

Writes a body chunk.

write(
  chunk?: string | buffer.IOBuffer | ArrayBufferView,
  encoding?: string,
  callback?: () => void,
): boolean;
NameTypeDescription
optional chunkstring | buffer.IOBuffer | ArrayBufferView
optional encodingstring
optional callback() => void
Return Valuebooleanfalse once queued outbound bytes exceed the high-water mark.

class ClientResponse

The response delivered to a http.request() / http.get() callback.

Events: data, end, close, error.

Properties

Methods

arrayBuffer()

arrayBuffer(): Promise<ArrayBuffer>;
NameTypeDescription
Return ValuePromise<ArrayBuffer>

bytes()

bytes(): Promise<buffer.IOBuffer>;
NameTypeDescription
Return ValuePromise<buffer.IOBuffer>

complete boolean read-only

headers Headers read-only

Merged, lowercased headers. See http.Headers.

off(event, listener)

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

pause()

pause(): void;

rawHeaders string[] read-only

Flat alternating [name, value, ...] wire-order header list.

readableFlowing boolean | null read-only

resume()

resume(): void;

statusCode number read-only

statusMessage string read-only

text()

text(): Promise<string>;
NameTypeDescription
Return ValuePromise<string>

url string read-only

function createServer

Creates an HTTP http.Server.

function createServer(requestListener?: RequestListener): Server;
NameTypeDescription
optional requestListenerRequestListener
Return ValueServer

function get

Starts a client GET request, calling end() automatically.

function get(
  optionsOrUrl: string | { href: string } | RequestOptions,
  callback?: (res: ClientResponse) => void,
): ClientRequest;
NameTypeDescription
optionsOrUrlstring | { href: string } | RequestOptions
optional callback(res: ClientResponse) => void
Return ValueClientRequest

globalAgentAgent

The shared default http.Agent.

globalAgent: Agent

type Headers

Merged, lowercased header view. Repeated set-cookie values are arrays; other repeated headers are merged into a single string.

type Headers = Record<string, string | string[]>;

class IncomingMessage

An incoming server request (the first argument to a request listener).

Events: data, end, close, error.

Properties

Methods

arrayBuffer()

Buffers the full body as an ArrayBuffer.

arrayBuffer(): Promise<ArrayBuffer>;
NameTypeDescription
Return ValuePromise<ArrayBuffer>

bytes()

Buffers the full body as bytes.

bytes(): Promise<buffer.IOBuffer>;
NameTypeDescription
Return ValuePromise<buffer.IOBuffer>

complete boolean read-only

destroyed boolean read-only

headers Headers read-only

Merged, lowercased headers. See http.Headers.

httpVersion string read-only

json()

Buffers and parses the full body as JSON.

json(): Promise<any>;
NameTypeDescription
Return ValuePromise<any>

localAddress string read-only

localPort number read-only

method string read-only

off(event, listener)

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

pause()

Pauses data event flow.

pause(): void;

rawHeaders string[] read-only

Flat alternating [name, value, ...] wire-order header list.

readableFlowing boolean | null read-only

remoteAddress string read-only

remotePort number read-only

resume()

Resumes data event flow.

resume(): void;

socket any read-only

text()

Buffers the full body as a UTF-8 string.

text(): Promise<string>;
NameTypeDescription
Return ValuePromise<string>

url string read-only

function request

Starts a client request.

function request(
  optionsOrUrl: string | { href: string } | RequestOptions,
  callback?: (res: ClientResponse) => void,
): ClientRequest;
NameTypeDescription
optionsOrUrlstring | { href: string } | RequestOptions
optional callback(res: ClientResponse) => void
Return ValueClientRequest

type RequestListener

Callback function to handle incoming requests.

type RequestListener = (req: IncomingMessage, res: ServerResponse) => void;

interface RequestOptions

Options accepted by http.request() / http.get().

Properties

agent any

A keep-alive http.Agent for pooled connection reuse.

decompress boolean

Opt in to transparent gzip/deflate decoding (default false).

headers Record<string, string | string[]>

host string

hostname string

method string

path string

port number

protocol string

signal AbortSignal

An AbortSignal that aborts the request.

timeout number

Abort the request after this many milliseconds.

class Server

An HTTP/1.1 server. Accepts a request listener and supports upgrade for WebSocket handling.

Events: listening, request, upgrade, close, error.

Methods

address()

address(): AddressInfo;
NameTypeDescription
Return ValueAddressInfo

close(callback)

close(callback?: () => void): this;
NameTypeDescription
optional callback() => void
Return Valuethis

listen(port, host, callback)

listen(port: number, host?: string, callback?: () => void): this;
NameTypeDescription
portnumber
optional hoststring
optional callback() => void
Return Valuethis

listen(options, callback)

listen(
  options: { port?: number; host?: string },
  callback?: () => void,
): this;
NameTypeDescription
optionsobjectoptional options.port number
optional options.host string
optional callback() => void
Return Valuethis

off(event, listener)

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

class ServerResponse

A server response (the second argument to a request listener).

Events: drain, close, error.

Properties

Methods

end(chunk, encoding, callback)

Writes an optional final chunk and ends the response.

end(
  chunk?: string | buffer.IOBuffer | ArrayBufferView,
  encoding?: string,
  callback?: () => void,
): void;
NameTypeDescription
optional chunkstring | buffer.IOBuffer | ArrayBufferView
optional encodingstring
optional callback() => void

getHeader(name)

Returns a previously set header value.

getHeader(name: string): string | string[] | undefined;
NameTypeDescription
namestring
Return Valuestring | string[] | undefined

headersSent boolean read-only

off(event, listener)

off(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

on(event, listener)

on(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

once(event, listener)

once(event: string, listener: (...args: any[]) => void): this;
NameTypeDescription
eventstring
listener(...args: any[]) => void
Return Valuethis

removeHeader(name)

Removes a previously set header.

removeHeader(name: string): void;
NameTypeDescription
namestring

setHeader(name, value)

Sets a response header. Array values emit repeated header lines.

setHeader(name: string, value: string | string[]): void;
NameTypeDescription
namestring
valuestring | string[]

statusCode number

statusMessage string

writableEnded boolean read-only

write(chunk, encoding, callback)

Writes a body chunk.

write(
  chunk?: string | buffer.IOBuffer | ArrayBufferView,
  encoding?: string,
  callback?: () => void,
): boolean;
NameTypeDescription
optional chunkstring | buffer.IOBuffer | ArrayBufferView
optional encodingstring
optional callback() => void
Return Valuebooleanfalse once queued outbound bytes exceed the high-water mark.

writeHead(statusCode, statusMessageOrHeaders, headers)

Writes the status line and headers.

writeHead(
  statusCode: number,
  statusMessageOrHeaders?: string | Record<string, string | string[]>,
  headers?: Record<string, string | string[]>,
): this;
NameTypeDescription
statusCodenumber
optional statusMessageOrHeadersstring | Record<string, string | string[]>
optional headersRecord<string, string | string[]>
Return Valuethis