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
| Parameter | Type | Description |
|---|---|---|
| optional options | object | optional 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()
- destroy(error)
- end(chunk, encoding, callback)
- getHeader(name)
- off(event, listener)
- on(event, listener)
- once(event, listener)
- removeHeader(name)
- setHeader(name, value)
- setTimeout(timeout, callback)
- write(chunk, encoding, callback)
abort()
Aborts the request.
abort(): void;
destroy(error)
Destroys the request, optionally with an error.
destroy(error?: Error): void;
| Name | Type | Description |
|---|---|---|
| optional error | Error |
end(chunk, encoding, callback)
Writes an optional final chunk and finishes the request.
end(
chunk?: string | buffer.IOBuffer | ArrayBufferView,
encoding?: string,
callback?: () => void,
): void;
| Name | Type | Description |
|---|---|---|
| optional chunk | string | buffer.IOBuffer | ArrayBufferView | |
| optional encoding | string | |
| optional callback | () => void |
getHeader(name)
Returns a previously set header value.
getHeader(name: string): string | string[] | undefined;
| Name | Type | Description |
|---|---|---|
| name | string | |
| Return Value | string | string[] | undefined |
off(event, listener)
off(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
removeHeader(name)
Removes a previously set header.
removeHeader(name: string): void;
| Name | Type | Description |
|---|---|---|
| name | string |
setHeader(name, value)
Sets a request header. Array values emit repeated header lines.
setHeader(name: string, value: string | string[]): void;
| Name | Type | Description |
|---|---|---|
| name | string | |
| value | string | string[] |
setTimeout(timeout, callback)
Sets a per-request timeout.
setTimeout(timeout: number, callback?: () => void): this;
| Name | Type | Description |
|---|---|---|
| timeout | number | |
| optional callback | () => void | |
| Return Value | this |
write(chunk, encoding, callback)
Writes a body chunk.
write(
chunk?: string | buffer.IOBuffer | ArrayBufferView,
encoding?: string,
callback?: () => void,
): boolean;
| Name | Type | Description |
|---|---|---|
| optional chunk | string | buffer.IOBuffer | ArrayBufferView | |
| optional encoding | string | |
| optional callback | () => void | |
| Return Value | boolean | false 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()
- bytes()
- off(event, listener)
- on(event, listener)
- once(event, listener)
- pause()
- resume()
- text()
arrayBuffer()
arrayBuffer(): Promise<ArrayBuffer>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<ArrayBuffer> |
bytes()
bytes(): Promise<buffer.IOBuffer>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<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;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
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>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<string> |
url string read-only
function createServer
Creates an HTTP http.Server.
function createServer(requestListener?: RequestListener): Server;
| Name | Type | Description |
|---|---|---|
| optional requestListener | RequestListener | |
| Return Value | Server |
function get
Starts a client GET request, calling end() automatically.
function get(
optionsOrUrl: string | { href: string } | RequestOptions,
callback?: (res: ClientResponse) => void,
): ClientRequest;
| Name | Type | Description |
|---|---|---|
| optionsOrUrl | string | { href: string } | RequestOptions | |
| optional callback | (res: ClientResponse) => void | |
| Return Value | ClientRequest |
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
- complete
- destroyed
- headers
- httpVersion
- localAddress
- localPort
- method
- rawHeaders
- readableFlowing
- remoteAddress
- remotePort
- socket
- url
Methods
- arrayBuffer()
- bytes()
- json()
- off(event, listener)
- on(event, listener)
- once(event, listener)
- pause()
- resume()
- text()
arrayBuffer()
Buffers the full body as an ArrayBuffer.
arrayBuffer(): Promise<ArrayBuffer>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<ArrayBuffer> |
bytes()
Buffers the full body as bytes.
bytes(): Promise<buffer.IOBuffer>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<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>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<any> |
localAddress string read-only
localPort number read-only
method string read-only
off(event, listener)
off(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
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>;
| Name | Type | Description |
|---|---|---|
| Return Value | Promise<string> |
url string read-only
function request
Starts a client request.
function request(
optionsOrUrl: string | { href: string } | RequestOptions,
callback?: (res: ClientResponse) => void,
): ClientRequest;
| Name | Type | Description |
|---|---|---|
| optionsOrUrl | string | { href: string } | RequestOptions | |
| optional callback | (res: ClientResponse) => void | |
| Return Value | ClientRequest |
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()
- close(callback)
- listen(port, host, callback)
- listen(options, callback)
- off(event, listener)
- on(event, listener)
- once(event, listener)
address()
address(): AddressInfo;
| Name | Type | Description |
|---|---|---|
| Return Value | AddressInfo |
close(callback)
close(callback?: () => void): this;
| Name | Type | Description |
|---|---|---|
| optional callback | () => void | |
| Return Value | this |
listen(port, host, callback)
listen(port: number, host?: string, callback?: () => void): this;
| Name | Type | Description |
|---|---|---|
| port | number | |
| optional host | string | |
| optional callback | () => void | |
| Return Value | this |
listen(options, callback)
listen(
options: { port?: number; host?: string },
callback?: () => void,
): this;
| Name | Type | Description |
|---|---|---|
| options | object | optional options.port number optional options.host string |
| optional callback | () => void | |
| Return Value | this |
off(event, listener)
off(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
class ServerResponse
A server response (the second argument to a request listener).
Events: drain, close, error.
Properties
Methods
- end(chunk, encoding, callback)
- getHeader(name)
- off(event, listener)
- on(event, listener)
- once(event, listener)
- removeHeader(name)
- setHeader(name, value)
- write(chunk, encoding, callback)
- writeHead(statusCode, statusMessageOrHeaders, headers)
end(chunk, encoding, callback)
Writes an optional final chunk and ends the response.
end(
chunk?: string | buffer.IOBuffer | ArrayBufferView,
encoding?: string,
callback?: () => void,
): void;
| Name | Type | Description |
|---|---|---|
| optional chunk | string | buffer.IOBuffer | ArrayBufferView | |
| optional encoding | string | |
| optional callback | () => void |
getHeader(name)
Returns a previously set header value.
getHeader(name: string): string | string[] | undefined;
| Name | Type | Description |
|---|---|---|
| name | string | |
| Return Value | string | string[] | undefined |
headersSent boolean read-only
off(event, listener)
off(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
on(event, listener)
on(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
once(event, listener)
once(event: string, listener: (...args: any[]) => void): this;
| Name | Type | Description |
|---|---|---|
| event | string | |
| listener | (...args: any[]) => void | |
| Return Value | this |
removeHeader(name)
Removes a previously set header.
removeHeader(name: string): void;
| Name | Type | Description |
|---|---|---|
| name | string |
setHeader(name, value)
Sets a response header. Array values emit repeated header lines.
setHeader(name: string, value: string | string[]): void;
| Name | Type | Description |
|---|---|---|
| name | string | |
| value | string | 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;
| Name | Type | Description |
|---|---|---|
| optional chunk | string | buffer.IOBuffer | ArrayBufferView | |
| optional encoding | string | |
| optional callback | () => void | |
| Return Value | boolean | false 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;
| Name | Type | Description |
|---|---|---|
| statusCode | number | |
| optional statusMessageOrHeaders | string | Record<string, string | string[]> | |
| optional headers | Record<string, string | string[]> | |
| Return Value | this |