ProxyForge Download

Axios Proxy Rotation in Node.js: Build a Scraping-Ready Proxy Pool

Axios Proxy Rotation in Node.js: Build a Scraping-Ready Proxy Pool

Web scraping and automation tasks often require rotating proxies to avoid being blocked by target servers. In this guide, we'll walk through setting up proxy rotation using Axios in a Node.js environment. We'll use the httpsAgent with socks-proxy-agent for handling different proxy types, manage per-request proxy objects, implement a rotation loop with failure tracking, and test free proxies.

Introduction

Axios is a popular HTTP client for making requests from Node.js applications. When combined with proxy management tools, it can be incredibly powerful for web scraping tasks. In this guide, we'll focus on setting up Axios to rotate proxies automatically, ensuring that each request uses a different proxy from the pool.

Prerequisites

Setting Up Axios with httpsAgent

First, we need to set up Axios to use a proxy. We'll use the httpsAgent option provided by Axios, which requires an agent compatible with HTTP/HTTPS requests.

Installing Required Packages

Run the following command to install Axios and socks-proxy-agent:

npm install axios socks-proxy-agent

Basic Proxy Setup

Here’s how you can set up a basic proxy using Axios:

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

// Define your proxy string
const proxyString = 'http://username:password@proxy-ip:port';

// Create an instance of HttpsProxyAgent
const agent = new HttpsProxyAgent(proxyString);

// Make a request using Axios with the proxy
axios.get('https://example.com', { httpsAgent: agent })
  .then(response => {
    console.log(response.data);
  })
  .catch(error => {
    console.error(error);
  });

Proxy Rotation with Failure Tracking

To handle multiple proxies and rotate them efficiently, we need a mechanism to track which proxy has failed and rotate through the pool.

Implementing Proxy Rotation

Let’s assume you have a list of proxies. We’ll create a function to rotate through these proxies and handle failures:

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

// List of proxies
let proxyList = [
  'http://proxy1:port',
  'http://proxy2:port',
  'http://proxy3:port'
];

// Function to create an agent from a proxy string
function createProxyAgent(proxy) {
  return new HttpsProxyAgent(proxy);
}

// Function to make a request with rotation
async function makeRequestWithRotation(url) {
  for (let i = 0; i < proxyList.length; i++) {
    const proxy = proxyList[i];
    const agent = createProxyAgent(proxy);

    try {
      const response = await axios.get(url, { httpsAgent: agent });
      console.log(response.data);
      return;
    } catch (error) {
      console.error(`Failed with proxy ${proxy}:`, error.message);
      // Optionally remove failed proxy from list
      proxyList.splice(i, 1);
      i--; // Adjust index after removal
    }
  }

  throw new Error('All proxies failed');
}

// Usage
makeRequestWithRotation('https://example.com').catch(error => {
  console.error('Request failed:', error.message);
});

Testing Free Proxies

Free proxy lists can be unreliable, so it’s important to test them before using them in your application. We'll write a simple script to test the proxies and filter out the working ones.

Testing Proxies with Axios

const axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');

async function testProxies(proxies) {
  const workingProxies = [];

  for (let proxy of proxies) {
    const agent = new HttpsProxyAgent(proxy);

    try {
      await axios.get('http://ipinfo.io/json', { httpsAgent: agent, timeout: 5000 });
      console.log(`Proxy ${proxy} is working.`);
      workingProxies.push(proxy);
    } catch (error) {
      console.error(`Proxy ${proxy} failed:`, error.message);
    }
  }

  return workingProxies;
}

// Example usage
const proxiesToTest = [
  'http://proxy1:port',
  'http://proxy2:port',
  // Add more proxies here
];

testProxies(proxiesToTest).then(workingProxies => {
  console.log('Working Proxies:', workingProxies);
}).catch(error => {
  console.error('Proxy testing failed:', error.message);
});

Residential Proxies vs DIY

While DIY solutions like the ones outlined above can work for basic use cases, they come with limitations. Residential proxies from providers like Bright Data or SmartProxy offer several advantages:

If you find managing a proxy pool manually becomes cumbersome or if you need more reliable proxies, consider using services like Bright Data or SmartProxy. They offer high-quality proxies that can significantly improve the success rate of your scraping tasks.

Conclusion

In this guide, we’ve covered setting up Axios for proxy rotation in Node.js, handling multiple proxies with failure tracking, and testing free proxies to ensure reliability. While DIY solutions are powerful and flexible, sometimes it’s worth considering professional services for better performance and support.

If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription:

https://proxyforge.dev/#download

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →