node:https
Node-compatible HTTPS (HTTP/1.1 over TLS) client and server APIs (node:https).
Available in vjs only (not available in vjs-lite). Enabled by default
unless --no-node-compat is passed.
import ... from "node:https"
node-compat (enabled by default in vjs; absent in vjs-lite; disable with --no-node-compat)Example
Section titled “Example”import { createServer } from "node:https";
const server = createServer({ key: "...", cert: "..." }, (_req, res) => { res.end("secure ok");});server.listen(0, "127.0.0.1");Functions
Section titled “Functions”createServer
Section titled “createServer”export function createServer(
options: ServerOptions,
requestListener?: RequestListener,
): Server;
export function createServer(requestListener?: RequestListener): Server;
Create a new HTTPS Server.
createServer(options, requestListener?)| Name | Type | Description |
|---|---|---|
options | ServerOptions | TLS certificates and HTTP server configuration. |
requestListener optional | RequestListener | Optional "request" event listener. |
Server — New HTTPS Server.createServer(requestListener?)Create a new HTTPS Server with an optional "request" listener.
| Name | Type | Description |
|---|---|---|
requestListener optional | RequestListener | Optional "request" event listener. |
Server — New HTTPS Server.request
Section titled “request”export function request(
options: RequestOptions | string | import("node:url").URL,
callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
export function request(
url: string | import("node:url").URL,
options: RequestOptions,
callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
Initiate an HTTPS request using options.
request(options, callback?)| Name | Type | Description |
|---|---|---|
options | RequestOptions | string | import("node:url").URL | Request options or URL string. |
callback optional | (res: import("node:http").IncomingMessage) => void | Optional "response" listener. |
import("node:http").ClientRequest — Outgoing ClientRequest.request(url, options, callback?)Initiate an HTTPS request to url with explicit options.
| Name | Type | Description |
|---|---|---|
url | string | import("node:url").URL | Target URL string or URL. |
options | RequestOptions | Request options merged over url. |
callback optional | (res: import("node:http").IncomingMessage) => void | Optional "response" listener. |
import("node:http").ClientRequest — Outgoing ClientRequest.export function get(
options: RequestOptions | string | import("node:url").URL,
callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
export function get(
url: string | import("node:url").URL,
options: RequestOptions,
callback?: (res: import("node:http").IncomingMessage) => void,
): import("node:http").ClientRequest;
Convenience wrapper around request() that forces method "GET" and
automatically calls .end().
get(options, callback?)| Name | Type | Description |
|---|---|---|
options | RequestOptions | string | import("node:url").URL | Request options or URL string. |
callback optional | (res: import("node:http").IncomingMessage) => void | Optional "response" listener. |
import("node:http").ClientRequest — Outgoing ClientRequest.get(url, options, callback?)Convenience "GET" request to url with explicit options.
| Name | Type | Description |
|---|---|---|
url | string | import("node:url").URL | Target URL string or URL. |
options | RequestOptions | Request options merged over url. |
callback optional | (res: import("node:http").IncomingMessage) => void | Optional "response" listener. |
import("node:http").ClientRequest — Outgoing ClientRequest.Classes
Section titled “Classes”export class Agent {
maxSockets: number;
maxTotalSockets: number;
maxFreeSockets: number;
readonly sockets: Record<string, unknown[]>;
readonly freeSockets: Record<string, unknown[]>;
readonly requests: Record<string, import("node:http").ClientRequest[]>;
constructor(options?: AgentOptions);
destroy(): void;
getName(options?: RequestOptions): string;
}
HTTPS connection-pooling agent managing TLS sockets.
constructor(options?: AgentOptions);
Construct a new HTTPS Agent.
| Name | Type | Description |
|---|---|---|
options optional | AgentOptions | Optional pooling and TLS configuration. |
| Property | Type | Description | Since |
|---|---|---|---|
maxSockets | number | Maximum concurrent sockets per host key. | v0.0.0-alpha.2 |
maxTotalSockets | number | Maximum total concurrent sockets across all hosts. | v0.0.0-alpha.2 |
maxFreeSockets | number | Maximum idle keep-alive sockets retained per host key. | v0.0.0-alpha.2 |
sockets readonly | Record<string, unknown[]> | Active sockets keyed by destination string. | v0.0.0-alpha.2 |
freeSockets readonly | Record<string, unknown[]> | Idle keep-alive sockets keyed by destination string. | v0.0.0-alpha.2 |
requests readonly | Record<string, import("node:http").ClientRequest[]> | Queued pending requests keyed by destination string. | v0.0.0-alpha.2 |
Agent.destroy
Section titled “Agent.destroy”destroy(): void;
Destroy all sockets currently managed by this agent.
voidAgent.getName
Section titled “Agent.getName”getName(options?: RequestOptions): string;
Return the canonical pool key string for options.
| Name | Type | Description |
|---|---|---|
options optional | RequestOptions | — |
stringServer
Section titled “Server”export class Server {
readonly listening: boolean;
headersTimeout: number;
keepAliveTimeout: number;
maxHeadersCount: number | null;
requestTimeout: number;
timeout: number;
constructor(requestListener?: RequestListener);
constructor(options: ServerOptions, requestListener?: RequestListener);
listen(port?: number, hostname?: string, listeningListener?: () => void): this;
listen(port?: number, listeningListener?: () => void): this;
listen(
options: { port?: number; host?: string; backlog?: number; path?: string },
listeningListener?: () => void,
): this;
close(callback?: (err?: Error) => void): this;
closeAllConnections(): void;
closeIdleConnections(): void;
setTimeout(msecs?: number, callback?: () => void): this;
setSecureContext(options: import("node:tls").SecureContextOptions): void;
addContext(hostname: string, context: import("node:tls").SecureContextOptions): void;
address(): { address: string; family: string; port: number } | string | null;
ref(): this;
unref(): this;
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
emit(eventName: string | symbol, ...args: any[]): boolean;
[Symbol.asyncDispose](): Promise<void>;
}
HTTPS server accepting TLS connections and dispatching HTTP/1.1 requests.
constructor(requestListener?: RequestListener);
constructor(options: ServerOptions, requestListener?: RequestListener);
Construct an HTTPS Server with an optional "request" listener.
new Server(requestListener?)| Name | Type | Description |
|---|---|---|
requestListener optional | RequestListener | Callback invoked for each HTTPS request. |
new Server(options, requestListener?)Construct an HTTPS Server with TLS options and optional listener.
| Name | Type | Description |
|---|---|---|
options | ServerOptions | TLS certificates and HTTP server configuration. |
requestListener optional | RequestListener | Callback invoked for each HTTPS request. |
| Property | Type | Description | Since |
|---|---|---|---|
listening readonly | boolean | true while the server is listening for connections. | v0.0.0-alpha.2 |
headersTimeout | number | Maximum milliseconds to wait for incoming headers. | v0.0.0-alpha.2 |
keepAliveTimeout | number | Keep-alive timeout in milliseconds. | v0.0.0-alpha.2 |
maxHeadersCount | number | null | Maximum number of headers parsed per request. | v0.0.0-alpha.2 |
requestTimeout | number | Request timeout in milliseconds. | v0.0.0-alpha.2 |
timeout | number | Inactivity socket timeout in milliseconds. | v0.0.0-alpha.2 |
Server.listen
Section titled “Server.listen”listen(port?: number, hostname?: string, listeningListener?: () => void): this;
listen(port?: number, listeningListener?: () => void): this;
listen(
options: { port?: number; host?: string; backlog?: number; path?: string },
listeningListener?: () => void,
): this;
Start listening for HTTPS connections on port.
listen(port?, hostname?, listeningListener?)| Name | Type | Description |
|---|---|---|
port optional | number | — |
hostname optional | string | — |
listeningListener optional | () => void | — |
thislisten(port?, listeningListener?)Start listening on port with optional listener.
| Name | Type | Description |
|---|---|---|
port optional | number | — |
listeningListener optional | () => void | — |
thislisten(options, listeningListener?)Start listening using explicit options.
| Name | Type | Description |
|---|---|---|
options | { port?: number; host?: string; backlog?: number; path?: string } | — |
listeningListener optional | () => void | — |
thisServer.close
Section titled “Server.close”close(callback?: (err?: Error) => void): this;
Stop accepting new connections and close once active requests finish.
| Name | Type | Description |
|---|---|---|
callback optional | (err?: Error) => void | — |
thisServer.closeAllConnections
Section titled “Server.closeAllConnections”closeAllConnections(): void;
Immediately close all connected sockets.
voidServer.closeIdleConnections
Section titled “Server.closeIdleConnections”closeIdleConnections(): void;
Close all idle keep-alive sockets.
voidServer.setTimeout
Section titled “Server.setTimeout”setTimeout(msecs?: number, callback?: () => void): this;
Set default socket inactivity timeout.
| Name | Type | Description |
|---|---|---|
msecs optional | number | — |
callback optional | () => void | — |
thisServer.setSecureContext
Section titled “Server.setSecureContext”setSecureContext(options: import("node:tls").SecureContextOptions): void;
Replace the server’s default TLS credentials and options.
| Name | Type | Description |
|---|---|---|
options | import("node:tls").SecureContextOptions | — |
voidServer.addContext
Section titled “Server.addContext”addContext(hostname: string, context: import("node:tls").SecureContextOptions): void;
Register an SNI hostname-specific SecureContextOptions.
| Name | Type | Description |
|---|---|---|
hostname | string | — |
context | import("node:tls").SecureContextOptions | — |
voidServer.address
Section titled “Server.address”address(): { address: string; family: string; port: number } | string | null;
Return bound local address metadata or null.
{ address: string; family: string; port: number } | string | nullServer.ref
Section titled “Server.ref”ref(): this;
Keep the event loop active while listening.
thisServer.unref
Section titled “Server.unref”unref(): this;
Allow the event loop to exit if the server is the only active handle.
thisServer.on
Section titled “Server.on”on(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.once
Section titled “Server.once”once(eventName: string | symbol, listener: (...args: any[]) => void): this;
Register a one-time event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.off
Section titled “Server.off”off(eventName: string | symbol, listener: (...args: any[]) => void): this;
Remove an event listener.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
listener | (...args: any[]) => void | — |
thisServer.emit
Section titled “Server.emit”emit(eventName: string | symbol, ...args: any[]): boolean;
Synchronously emit an event.
| Name | Type | Description |
|---|---|---|
eventName | string | symbol | — |
...args | any[] | — |
booleanServer.[Symbol.asyncDispose]
Section titled “Server.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the server asynchronously when disposed via await using.
Promise<void>Interfaces
Section titled “Interfaces”AbortSignalLike
Section titled “AbortSignalLike”export interface AbortSignalLike {
readonly aborted: boolean;
readonly reason?: unknown;
addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
removeEventListener?(type: string, listener: (...args: any[]) => void): void;
}
Minimal abort-signal contract accepted by HTTPS request options.
| Property | Type | Description | Since |
|---|---|---|---|
aborted readonly | boolean | Whether the signal has already been aborted. | v0.0.0-alpha.2 |
reason readonly optional | unknown | Optional abort reason value. | v0.0.0-alpha.2 |
AbortSignalLike.addEventListener
Section titled “AbortSignalLike.addEventListener”addEventListener(type: string, listener: (...args: any[]) => void, options?: { once?: boolean }): void;
Register an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
options optional | { once?: boolean } | — |
voidAbortSignalLike.removeEventListener
Section titled “AbortSignalLike.removeEventListener”removeEventListener?(type: string, listener: (...args: any[]) => void): void;
Unregister an "abort" listener.
| Name | Type | Description |
|---|---|---|
type | string | — |
listener | (...args: any[]) => void | — |
voidRequestOptions
Section titled “RequestOptions”export interface RequestOptions {
protocol?: string | null;
host?: string | null;
hostname?: string | null;
family?: number;
port?: number | string | null;
localAddress?: string;
method?: string;
path?: string | null;
headers?: import("node:http").OutgoingHttpHeaders;
auth?: string | null;
agent?: Agent | boolean;
timeout?: number;
signal?: AbortSignalLike;
ca?: string | Uint8Array | Array<string | Uint8Array>;
cert?: string | Uint8Array | Array<string | Uint8Array>;
key?: string | Uint8Array | Array<string | Uint8Array>;
passphrase?: string;
pfx?: string | Uint8Array | Array<string | Uint8Array>;
ciphers?: string;
rejectUnauthorized?: boolean;
servername?: string;
minVersion?: import("node:tls").SecureVersion;
maxVersion?: import("node:tls").SecureVersion;
checkServerIdentity?: typeof import("node:tls").checkServerIdentity;
}
Options accepted by https.request(), https.get(), and https.createServer().
| Property | Type | Description | Since |
|---|---|---|---|
protocol optional | string | null | URL protocol scheme ("https:"). | v0.0.0-alpha.2 |
host optional | string | null | Target host or IP address. | v0.0.0-alpha.2 |
hostname optional | string | null | Target hostname (preferred over host). | v0.0.0-alpha.2 |
family optional | number | IP address family (4 or 6). | v0.0.0-alpha.2 |
port optional | number | string | null | Target TCP port (default 443). | v0.0.0-alpha.2 |
localAddress optional | string | Local interface IP address to bind. | v0.0.0-alpha.2 |
method optional | string | HTTP method ("GET", "POST", etc.). | v0.0.0-alpha.2 |
path optional | string | null | Request path and query string (default "/"). | v0.0.0-alpha.2 |
headers optional | import("node:http").OutgoingHttpHeaders | Outgoing HTTP headers. | v0.0.0-alpha.2 |
auth optional | string | null | Basic authentication "user:password" string. | v0.0.0-alpha.2 |
agent optional | Agent | boolean | Connection-pooling Agent, or false to bypass pooling. | v0.0.0-alpha.2 |
timeout optional | number | Socket inactivity timeout in milliseconds. | v0.0.0-alpha.2 |
signal optional | AbortSignalLike | Optional abort signal cancelling the in-flight request. | v0.0.0-alpha.2 |
ca optional | string | Uint8Array | Array<string | Uint8Array> | Trusted CA certificates (PEM string or bytes). | v0.0.0-alpha.2 |
cert optional | string | Uint8Array | Array<string | Uint8Array> | Client certificate chain in PEM format. | v0.0.0-alpha.2 |
key optional | string | Uint8Array | Array<string | Uint8Array> | Client private key in PEM format. | v0.0.0-alpha.2 |
passphrase optional | string | Passphrase protecting private keys. | v0.0.0-alpha.2 |
pfx optional | string | Uint8Array | Array<string | Uint8Array> | PKCS#12 certificate/key bundle. | v0.0.0-alpha.2 |
ciphers optional | string | Cipher suite specification string. | v0.0.0-alpha.2 |
rejectUnauthorized optional | boolean | Reject invalid server certificates (default true). | v0.0.0-alpha.2 |
servername optional | string | SNI server name for TLS handshake and verification. | v0.0.0-alpha.2 |
minVersion optional | import("node:tls").SecureVersion | Minimum allowed TLS protocol version. | v0.0.0-alpha.2 |
maxVersion optional | import("node:tls").SecureVersion | Maximum allowed TLS protocol version. | v0.0.0-alpha.2 |
checkServerIdentity optional | typeof import("node:tls").checkServerIdentity | Custom hostname/certificate verification callback. | v0.0.0-alpha.2 |
Type Aliases
Section titled “Type Aliases”RequestListener
Section titled “RequestListener”export type RequestListener = (
req: import("node:http").IncomingMessage,
res: import("node:http").ServerResponse,
) => void;
HTTPS request listener callback signature.
AgentOptions
Section titled “AgentOptions”export type AgentOptions = import("node:http").AgentOptions & {
/** Trusted CA certificates (PEM string or bytes). @since 0.0.0-alpha.2 */
ca?: string | Uint8Array | Array<string | Uint8Array>;
/** Client certificate chain in PEM format. @since 0.0.0-alpha.2 */
cert?: string | Uint8Array | Array<string | Uint8Array>;
/** Client private key in PEM format. @since 0.0.0-alpha.2 */
key?: string | Uint8Array | Array<string | Uint8Array>;
/** Passphrase for encrypted private keys or PKCS#12 bundles. @since 0.0.0-alpha.2 */
passphrase?: string;
/** PKCS#12 certificate/key bundle. @since 0.0.0-alpha.2 */
pfx?: string | Uint8Array | Array<string | Uint8Array>;
/** Cipher suite specification string. @since 0.0.0-alpha.2 */
ciphers?: string;
/** Reject peers whose certificate fails trust verification (default `true`). @since 0.0.0-alpha.2 */
rejectUnauthorized?: boolean;
/** SNI server name override. @since 0.0.0-alpha.2 */
servername?: string;
/** Maximum number of TLS sessions cached by this agent. @since 0.0.0-alpha.2 */
maxCachedSessions?: number;
/** Minimum allowed TLS protocol version. @since 0.0.0-alpha.2 */
minVersion?: import("node:tls").SecureVersion;
/** Maximum allowed TLS protocol version. @since 0.0.0-alpha.2 */
maxVersion?: import("node:tls").SecureVersion;
};
Options accepted by new https.Agent(options).
ServerOptions
Section titled “ServerOptions”export type ServerOptions = import("node:http").ServerOptions &
import("node:tls").TlsOptions;
Options accepted by https.createServer() and new https.Server().
Constants
Section titled “Constants”globalAgent
Section titled “globalAgent”export const globalAgent: Agent;
Default global HTTPS Agent used when no custom agent is supplied.
Default Export
Section titled “Default Export”default export
Section titled “default export”const _default: {
/** `Agent` constructor. @since 0.0.0-alpha.2 */
Agent: typeof Agent;
/** `Server` constructor. @since 0.0.0-alpha.2 */
Server: typeof Server;
/** Create an HTTPS server. @since 0.0.0-alpha.2 */
createServer: typeof createServer;
/** Issue an HTTPS GET request. @since 0.0.0-alpha.2 */
get: typeof get;
/** Default global HTTPS `Agent`. @since 0.0.0-alpha.2 */
readonly globalAgent: Agent;
/** Issue an HTTPS request. @since 0.0.0-alpha.2 */
request: typeof request;
};
export default _default;
Default export object providing the node:https API.
