agent-phin Node.js HTTP Client

1.0.4 · active · verified Wed Apr 22

agent-phin is an ultra-lightweight Node.js HTTP client, forked from the popular phin library, specifically designed to add robust HTTP agent support. It is currently at version 1.0.4 and appears to maintain a release cadence tied to the introduction of specific features, such as the agent support in v1.0.1. Its primary differentiator is its minimal footprint, being significantly smaller than many common HTTP clients like request, axios, or node-fetch, while still providing essential features like POST requests, JSON parsing, and configurable timeouts. The key addition over the original phin is the native capability to utilize custom `http.Agent` or `https.Agent` instances, enabling features like connection pooling (KeepAlive) or proxy integration via libraries like `https-proxy-agent`. It supports both Promise-based and unpromisified (callback-style) API calls, catering to different asynchronous programming preferences in Node.js environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to make a POST request using `agent-phin` with a `https.Agent` configured for KeepAlive, showcasing its primary differentiating feature for efficient connection management.

const p = require('agent-phin');
const https = require('https');

async function makeRequestWithAgent() {
  // Create a KeepAlive agent to reuse TCP connections
  const keepAliveAgent = new https.Agent({ keepAlive: true });

  try {
    const res = await p({
      url: 'https://httpbin.org/post',
      method: 'POST',
      data: {
        message: 'Hello from agent-phin with KeepAlive!'
      },
      agent: keepAliveAgent, // Pass the custom agent here
      parse: 'json' // Automatically parse the response body as JSON
    });

    console.log('Response status:', res.statusCode);
    console.log('Response body:', res.body);
  } catch (error) {
    console.error('Request failed:', error);
  } finally {
    // It's crucial to destroy the agent when it's no longer needed to prevent resource leaks.
    keepAliveAgent.destroy();
  }
}

makeRequestWithAgent();

view raw JSON →