HTTP Proxy Agent

9.0.0 · active · verified Tue Apr 21

`http-proxy-agent` provides a robust `http.Agent` implementation designed for making HTTP requests through an HTTP or HTTPS proxy server in Node.js environments. Currently at version 9.0.0, this library is part of the actively maintained `proxy-agents` monorepo, which generally sees updates aligned with Node.js LTS releases. Its primary function is to integrate seamlessly with Node's built-in `http` module, abstracting the proxy connection details. A key distinction is its specific focus on `http` module requests; for proxying `https` module requests, the companion `https-proxy-agent` package should be used. It supports standard `http.Agent` options and allows for custom headers to be sent to the proxy. This package is specifically designed for Node.js environments, requiring Node.js 20 or later for version 9.0.0.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create and use an `HttpProxyAgent` instance with Node.js's built-in `http.get` method to route an HTTP request through a specified proxy server.

import * as http from 'http';
import { HttpProxyAgent } from 'http-proxy-agent';

// Replace with your actual proxy URL and target URL
const proxyUrl = process.env.HTTP_PROXY_URL ?? 'http://127.0.0.1:3128';
const targetUrl = process.env.TARGET_URL ?? 'http://example.com/api/';

const agent = new HttpProxyAgent(proxyUrl);

http.get(targetUrl, { agent }, (res) => {
  console.log(`Proxying request to ${targetUrl} via ${proxyUrl}`);
  console.log('Response status:', res.statusCode);
  console.log('Response headers:', res.headers);
  // Log a portion of the response body to show it's working
  res.on('data', (chunk) => {
    process.stdout.write(chunk.toString().substring(0, 100) + '...');
  });
  res.on('end', () => {
    console.log('\n--- Request complete ---');
  });
}).on('error', (err) => {
  console.error('Request failed:', err.message);
});

view raw JSON →