How Axios Uses Node.js HTTP and HTTPS Modules

Axios is an isomorphic HTTP client that automatically adapts its transport layer depending on whether it is running in a web browser or a Node.js runtime. When executed in Node.js, Axios abstracts the runtime's native http and https modules to dispatch network requests, handle streaming data, manage SSL configurations, and process responses. Understanding this interaction reveals how Axios bridges its promise-based API with Node.js's event-driven, stream-based networking core.

The Adapter Architecture

Axios implements an adapter design pattern to achieve platform independence. During initialization, Axios evaluates the execution environment. When it detects a Node.js runtime (traditionally by verifying the existence of the process global and lack of browser-specific globals like XMLHttpRequest), it selects its internal Node adapter (axios/lib/adapters/http.js).

This adapter translates standard Axios request configurations into the options expected by the native http and https modules, delegating all actual network operations to Node.js.

Protocol Detection and Request Dispatch

When a request is initiated, the Node adapter parses the destination URL to determine the protocol:

  1. Protocol Routing: If the target URL uses http:, Axios routes the request through http.request(). If it uses https:, it invokes https.request(). Custom protocols or unsupported schemes trigger an immediate error.
  2. Options Mapping: The adapter maps the Axios configuration object to native Node.js request options. Properties such as headers, auth, timeout, proxy, and path parameters are serialized and formatted to conform to Node's RequestOptions interface.
  3. Dispatch: Axios calls the appropriate native function (http.request or https.request), which returns an instance of http.ClientRequest.

Payload Handling and Data Streaming

Node.js handles request and response bodies as streams. The Axios Node adapter manages the lifecycle of these streams:

Connection Management and Custom Agents

The native http and https modules rely on http.Agent and https.Agent to manage connection persistence (HTTP Keep-Alive), socket pooling, and TLS handshakes.

Axios allows developers to pass custom httpAgent and httpsAgent instances directly through its configuration. When provided, the adapter forwards these agents to the native request options. This mechanism enables fine-grained control over:

Timeouts, Cancellation, and Error Handling

The Node adapter translates native networking events into JavaScript Promises: