← Back to Blog

Setting Up Proxies in Node.js Applications: A Complete Guide with Code Examples

A detailed guide to integrating proxies in Node.js: from basic setup to advanced techniques for IP rotation and error handling.

πŸ“…February 14, 2026

When developing Node.js applications for scraping, automation, or working with APIs, there is often a need to use proxy servers. This allows bypassing geographical restrictions, distributing load, and avoiding IP bans. In this guide, we will explore all the ways to set up proxies in Node.jsβ€”from basic to advanced techniques with rotation and error handling.

Basic Concepts of Working with Proxies in Node.js

A proxy server in Node.js acts as an intermediary between your application and the target server. When you send an HTTP request through a proxy, your application first connects to the proxy server, which then forwards the request to the final destination. This allows you to hide your server's real IP address and use the proxy's IP address instead.

In Node.js, there are several basic ways to work with proxies depending on the library used for HTTP requests. The most popular options include:

  • Built-in http/https Modules β€” basic functionality without additional dependencies
  • Axios β€” a popular library with a convenient API and promise support
  • Got β€” a modern alternative with TypeScript support
  • node-fetch β€” an implementation of the Fetch API for Node.js
  • request β€” deprecated but still used library (not recommended for new projects)

Proxy servers support various protocols. The most commonly used with Node.js are:

Protocol Description Usage
HTTP Basic protocol for unencrypted connections Scraping, working with APIs without SSL
HTTPS Proxy with SSL/TLS encryption support Secure connections, working with protected APIs
SOCKS5 Universal protocol for any type of traffic WebSocket, UDP, complex scenarios

For scraping and automation tasks, developers often use residential proxies as they have real IP addresses of home users and are less likely to be blocked by target sites.

Setting Up Proxies via the Built-in http/https Module

Node.js provides built-in http and https modules for working with HTTP requests. To connect to a proxy, you can use the http-proxy-agent or https-proxy-agent libraries.

First, install the necessary packages:

npm install http-proxy-agent https-proxy-agent

Example of using an HTTP proxy with the built-in module:

const http = require('http');
const { HttpProxyAgent } = require('http-proxy-agent');

// Proxy settings
const proxyUrl = 'http://username:password@proxy-server.com:8080';
const agent = new HttpProxyAgent(proxyUrl);

// Request options
const options = {
  hostname: 'api.example.com',
  path: '/endpoint',
  method: 'GET',
  agent: agent,
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  }
};

// Making the request
const req = http.request(options, (res) => {
  let data = '';
  
  res.on('data', (chunk) => {
    data += chunk;
  });
  
  res.on('end', () => {
    console.log('Response:', data);
  });
});

req.on('error', (error) => {
  console.error('Request failed:', error.message);
});

req.end();

For HTTPS connections, use https-proxy-agent:

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

const proxyUrl = 'http://username:password@proxy-server.com:8080';
const agent = new HttpsProxyAgent(proxyUrl);

const options = {
  hostname: 'api.example.com',
  path: '/secure-endpoint',
  method: 'GET',
  agent: agent
};

https.get(options, (res) => {
  let data = '';
  
  res.on('data', (chunk) => {
    data += chunk;
  });
  
  res.on('end', () => {
    console.log('Secure response:', data);
  });
}).on('error', (error) => {
  console.error('HTTPS request failed:', error.message);
});

This approach provides maximum control over requests but requires more code for handling promises and errors. For most tasks, it is more convenient to use specialized libraries.

Working with Proxies in the Axios Library

Axios is one of the most popular libraries for HTTP requests in Node.js. It provides a convenient API for configuring proxies and automatically handles promises.

Installing Axios:

npm install axios

Basic proxy configuration in Axios looks like this:

const axios = require('axios');

// Method 1: Setting up the proxy in the request configuration
axios.get('https://api.example.com/data', {
  proxy: {
    protocol: 'http',
    host: 'proxy-server.com',
    port: 8080,
    auth: {
      username: 'your-username',
      password: 'your-password'
    }
  }
})
.then(response => {
  console.log('Data:', response.data);
})
.catch(error => {
  console.error('Error:', error.message);
});

For repeated use of the same proxy settings, it is convenient to create an Axios instance with pre-configured settings:

const axios = require('axios');

// Creating an instance with proxy settings
const axiosWithProxy = axios.create({
  proxy: {
    protocol: 'http',
    host: 'proxy-server.com',
    port: 8080,
    auth: {
      username: 'your-username',
      password: 'your-password'
    }
  },
  timeout: 10000,
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
  }
});

// Using the instance
async function fetchData() {
  try {
    const response = await axiosWithProxy.get('https://api.example.com/data');
    console.log('Response:', response.data);
    return response.data;
  } catch (error) {
    console.error('Request failed:', error.message);
    throw error;
  }
}

fetchData();

To work with HTTPS proxies via the CONNECT method, you can use additional agents:

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

const proxyUrl = 'http://username:password@proxy-server.com:8080';
const httpsAgent = new HttpsProxyAgent(proxyUrl);

const axiosInstance = axios.create({
  httpsAgent: httpsAgent,
  timeout: 15000
});

// Usage
axiosInstance.get('https://api.example.com/secure-data')
  .then(response => console.log(response.data))
  .catch(error => console.error(error.message));

Axios automatically handles redirects and supports interceptors for logging and error handling, making it an excellent choice for projects with active proxy usage.

Setting Up Proxies in got and node-fetch

Got is a modern alternative to Axios with excellent TypeScript support and advanced error handling capabilities. Node-fetch implements the standard Fetch API for Node.js.

Working with Proxies in Got

Installing Got and necessary agents:

npm install got https-proxy-agent

Example of setting up a proxy in Got:

const got = require('got');
const { HttpsProxyAgent } = require('https-proxy-agent');

const proxyUrl = 'http://username:password@proxy-server.com:8080';
const agent = {
  https: new HttpsProxyAgent(proxyUrl)
};

// Creating a Got instance with the proxy
const gotWithProxy = got.extend({
  agent: agent,
  timeout: {
    request: 10000
  },
  retry: {
    limit: 3,
    methods: ['GET', 'POST']
  }
});

// Usage
async function fetchWithGot() {
  try {
    const response = await gotWithProxy('https://api.example.com/data');
    console.log('Response:', JSON.parse(response.body));
  } catch (error) {
    console.error('Got request failed:', error.message);
  }
}

fetchWithGot();

Working with Proxies in node-fetch

Node-fetch requires explicit agent configuration to work with proxies:

npm install node-fetch https-proxy-agent
const fetch = require('node-fetch');
const { HttpsProxyAgent } = require('https-proxy-agent');

const proxyUrl = 'http://username:password@proxy-server.com:8080';
const agent = new HttpsProxyAgent(proxyUrl);

async function fetchWithProxy() {
  try {
    const response = await fetch('https://api.example.com/data', {
      agent: agent,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
      }
    });
    
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    
    const data = await response.json();
    console.log('Data:', data);
    return data;
  } catch (error) {
    console.error('Fetch failed:', error.message);
    throw error;
  }
}

fetchWithProxy();

Got provides richer functionality out of the box (automatic retries, timeouts, error handling), while node-fetch is closer to the standard Fetch API and is suitable for developers familiar with browser JavaScript.

Connecting SOCKS5 Proxies via socks-proxy-agent

SOCKS5 is a universal proxy protocol that operates at a lower level than HTTP/HTTPS. It supports any type of traffic, including UDP and WebSocket connections. To work with SOCKS5 in Node.js, the socks-proxy-agent library is used.

Installing the necessary packages:

npm install socks-proxy-agent

Example of using a SOCKS5 proxy with various libraries:

const { SocksProxyAgent } = require('socks-proxy-agent');
const https = require('https');

// Setting up SOCKS5 proxy with authentication
const proxyUrl = 'socks5://username:password@proxy-server.com:1080';
const agent = new SocksProxyAgent(proxyUrl);

const options = {
  hostname: 'api.example.com',
  path: '/endpoint',
  method: 'GET',
  agent: agent
};

https.get(options, (res) => {
  let data = '';
  
  res.on('data', (chunk) => {
    data += chunk;
  });
  
  res.on('end', () => {
    console.log('SOCKS5 response:', data);
  });
}).on('error', (error) => {
  console.error('SOCKS5 request failed:', error.message);
});

Using SOCKS5 with Axios:

const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');

const proxyUrl = 'socks5://username:password@proxy-server.com:1080';
const httpsAgent = new SocksProxyAgent(proxyUrl);
const httpAgent = new SocksProxyAgent(proxyUrl);

const axiosWithSocks = axios.create({
  httpAgent: httpAgent,
  httpsAgent: httpsAgent,
  timeout: 15000
});

async function fetchViaSocks5() {
  try {
    const response = await axiosWithSocks.get('https://api.example.com/data');
    console.log('Data via SOCKS5:', response.data);
    return response.data;
  } catch (error) {
    console.error('SOCKS5 request error:', error.message);
    throw error;
  }
}

fetchViaSocks5();

SOCKS5 proxies are especially useful for tasks that require working with non-standard protocols or when maximum flexibility is needed. Many providers offer mobile proxies with SOCKS5 support, allowing you to emulate mobile traffic for working with mobile APIs.

Implementing Proxy Rotation for Load Distribution

When scraping large volumes of data or working with APIs that have limits on the number of requests from a single IP, it is necessary to use proxy rotation. This allows distributing requests among multiple proxy servers and avoiding bans.

Example of implementing simple proxy rotation:

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

class ProxyRotator {
  constructor(proxyList) {
    this.proxyList = proxyList;
    this.currentIndex = 0;
  }
  
  // Get the next proxy in a circular manner
  getNextProxy() {
    const proxy = this.proxyList[this.currentIndex];
    this.currentIndex = (this.currentIndex + 1) % this.proxyList.length;
    return proxy;
  }
  
  // Get a random proxy
  getRandomProxy() {
    const randomIndex = Math.floor(Math.random() * this.proxyList.length);
    return this.proxyList[randomIndex];
  }
  
  // Create an Axios instance with the current proxy
  createAxiosInstance(useRandom = false) {
    const proxyUrl = useRandom ? this.getRandomProxy() : this.getNextProxy();
    const agent = new HttpsProxyAgent(proxyUrl);
    
    return axios.create({
      httpsAgent: agent,
      timeout: 10000,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
      }
    });
  }
}

// List of proxies
const proxyList = [
  'http://user1:pass1@proxy1.example.com:8080',
  'http://user2:pass2@proxy2.example.com:8080',
  'http://user3:pass3@proxy3.example.com:8080',
  'http://user4:pass4@proxy4.example.com:8080'
];

const rotator = new ProxyRotator(proxyList);

// Using rotation
async function fetchWithRotation(url) {
  const axiosInstance = rotator.createAxiosInstance();
  
  try {
    const response = await axiosInstance.get(url);
    console.log('Success with proxy');
    return response.data;
  } catch (error) {
    console.error('Request failed:', error.message);
    throw error;
  }
}

// Example of mass scraping with rotation
async function massiveParsing(urls) {
  const results = [];
  
  for (const url of urls) {
    try {
      const data = await fetchWithRotation(url);
      results.push({ url, success: true, data });
      
      // Delay between requests
      await new Promise(resolve => setTimeout(resolve, 1000));
    } catch (error) {
      results.push({ url, success: false, error: error.message });
    }
  }
  
  return results;
}

// Running the scraping
const urlsToParse = [
  'https://api.example.com/data/1',
  'https://api.example.com/data/2',
  'https://api.example.com/data/3',
  'https://api.example.com/data/4',
  'https://api.example.com/data/5'
];

massiveParsing(urlsToParse)
  .then(results => console.log('Parsing results:', results))
  .catch(error => console.error('Parsing error:', error));

A more advanced example with tracking the state of proxies and automatically excluding non-working ones:

class AdvancedProxyRotator {
  constructor(proxyList, maxFailures = 3) {
    this.proxyList = proxyList.map(url => ({
      url,
      failures: 0,
      active: true,
      lastUsed: null
    }));
    this.maxFailures = maxFailures;
    this.currentIndex = 0;
  }
  
  // Get an active proxy
  getActiveProxy() {
    const activeProxies = this.proxyList.filter(p => p.active);
    
    if (activeProxies.length === 0) {
      throw new Error('No active proxies available');
    }
    
    // Find the proxy that hasn't been used the longest
    const proxy = activeProxies.reduce((oldest, current) => {
      if (!oldest.lastUsed) return oldest;
      if (!current.lastUsed) return current;
      return current.lastUsed < oldest.lastUsed ? current : oldest;
    });
    
    proxy.lastUsed = Date.now();
    return proxy;
  }
  
  // Mark a proxy as successful
  markSuccess(proxyUrl) {
    const proxy = this.proxyList.find(p => p.url === proxyUrl);
    if (proxy) {
      proxy.failures = 0;
    }
  }
  
  // Mark a proxy as failed
  markFailure(proxyUrl) {
    const proxy = this.proxyList.find(p => p.url === proxyUrl);
    if (proxy) {
      proxy.failures++;
      if (proxy.failures >= this.maxFailures) {
        proxy.active = false;
        console.warn(`Proxy ${proxyUrl} deactivated after ${proxy.failures} failures`);
      }
    }
  }
  
  // Create an Axios instance with rotation
  async createAxiosInstance() {
    const proxy = this.getActiveProxy();
    const agent = new HttpsProxyAgent(proxy.url);
    
    return {
      instance: axios.create({
        httpsAgent: agent,
        timeout: 10000
      }),
      proxyUrl: proxy.url
    };
  }
  
  // Get proxy statistics
  getStats() {
    return {
      total: this.proxyList.length,
      active: this.proxyList.filter(p => p.active).length,
      inactive: this.proxyList.filter(p => !p.active).length,
      proxies: this.proxyList.map(p => ({
        url: p.url.replace(/\/\/.*@/, '//***@'), // Hide credentials
        active: p.active,
        failures: p.failures
      }))
    };
  }
}

// Using the advanced rotator
const advancedRotator = new AdvancedProxyRotator(proxyList);

async function fetchWithAdvancedRotation(url) {
  const { instance, proxyUrl } = await advancedRotator.createAxiosInstance();
  
  try {
    const response = await instance.get(url);
    advancedRotator.markSuccess(proxyUrl);
    return response.data;
  } catch (error) {
    advancedRotator.markFailure(proxyUrl);
    throw error;
  }
}

// Periodic statistics output
setInterval(() => {
  console.log('Proxy stats:', advancedRotator.getStats());
}, 30000);

This system automatically excludes non-working proxies from rotation and evenly distributes the load among active servers. This is critically important when working with large volumes of data.

Error Handling and Automatic Proxy Switching

When working with proxies, errors are inevitable: timeouts, connection refusals, and blocks from the target server. Proper error handling and automatic switching to another proxy enhance the reliability of the application.

Example of implementing a system with automatic retries and proxy switching:

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

class ResilientProxyClient {
  constructor(proxyList, maxRetries = 3) {
    this.proxyList = proxyList;
    this.maxRetries = maxRetries;
    this.currentProxyIndex = 0;
  }
  
  // Get the next proxy
  getNextProxy() {
    const proxy = this.proxyList[this.currentProxyIndex];
    this.currentProxyIndex = (this.currentProxyIndex + 1) % this.proxyList.length;
    return proxy;
  }
  
  // Determine the type of error
  isRetryableError(error) {
    // Errors that should trigger a retry
    const retryableCodes = ['ECONNRESET', 'ETIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED'];
    const retryableStatuses = [408, 429, 500, 502, 503, 504];
    
    if (error.code && retryableCodes.includes(error.code)) {
      return true;
    }
    
    if (error.response && retryableStatuses.includes(error.response.status)) {
      return true;
    }
    
    return false;
  }
  
  // Perform a request with automatic retries
  async request(url, options = {}, attempt = 1) {
    const proxyUrl = this.getNextProxy();
    const agent = new HttpsProxyAgent(proxyUrl);
    
    const config = {
      ...options,
      httpsAgent: agent,
      timeout: 10000,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
        ...options.headers
      }
    };
    
    try {
      console.log(`Attempt ${attempt}/${this.maxRetries} with proxy: ${proxyUrl.replace(/\/\/.*@/, '//***@')}`);
      const response = await axios.get(url, config);
      console.log(`Success on attempt ${attempt}`);
      return response.data;
    } catch (error) {
      console.error(`Attempt ${attempt} failed: ${error.message}`);
      
      // If the retry limit is reached
      if (attempt >= this.maxRetries) {
        console.error(`Max retries (${this.maxRetries}) reached for ${url}`);
        throw error;
      }
      
      // If the error is not retryable
      if (!this.isRetryableError(error)) {
        console.error(`Non-retryable error: ${error.message}`);
        throw error;
      }
      
      // Exponential delay before retrying
      const delay = Math.min(1000 * Math.pow(2, attempt - 1), 10000);
      console.log(`Waiting ${delay}ms before retry...`);
      await new Promise(resolve => setTimeout(resolve, delay));
      
      // Recursive retry with the next proxy
      return this.request(url, options, attempt + 1);
    }
  }
  
  // Batch processing of URLs with error handling
  async batchRequest(urls, concurrency = 3) {
    const results = [];
    const queue = [...urls];
    const active = [];
    
    while (queue.length > 0 || active.length > 0) {
      // Start new tasks until the concurrency limit is reached
      while (active.length < concurrency && queue.length > 0) {
        const url = queue.shift();
        const promise = this.request(url)
          .then(data => ({ url, success: true, data }))
          .catch(error => ({ url, success: false, error: error.message }))
          .finally(() => {
            const index = active.indexOf(promise);
            if (index > -1) active.splice(index, 1);
          });
        
        active.push(promise);
      }
      
      // Wait for at least one task to complete
      if (active.length > 0) {
        const result = await Promise.race(active);
        results.push(result);
      }
    }
    
    return results;
  }
}

// Usage
const proxyList = [
  'http://user1:pass1@proxy1.example.com:8080',
  'http://user2:pass2@proxy2.example.com:8080',
  'http://user3:pass3@proxy3.example.com:8080'
];

const client = new ResilientProxyClient(proxyList, 3);

// Single request
client.request('https://api.example.com/data')
  .then(data => console.log('Data:', data))
  .catch(error => console.error('Final error:', error.message));

// Batch processing
const urls = [
  'https://api.example.com/data/1',
  'https://api.example.com/data/2',
  'https://api.example.com/data/3',
  'https://api.example.com/data/4',
  'https://api.example.com/data/5'
];

client.batchRequest(urls, 3)
  .then(results => {
    const successful = results.filter(r => r.success).length;
    const failed = results.filter(r => !r.success).length;
    console.log(`Completed: ${successful} successful, ${failed} failed`);
    console.log('Results:', results);
  });

This implementation includes:

  • Automatic switching to the next proxy on error
  • Exponential delay between retries (1s, 2s, 4s, 8s, 10s max)
  • Error type determination for retry decisions
  • Concurrency control during batch processing
  • Detailed logging for debugging

Best Practices and Performance Optimization

When working with proxies in Node.js applications, it is advisable to follow these recommendations to ensure stability and performance:

1. Connection Management

Reuse HTTP agents instead of creating new ones for each request. This reduces overhead on establishing connections:

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

// Bad: creating an agent for each request
async function badExample(url) {
  const agent = new HttpsProxyAgent(proxyUrl);
  return axios.get(url, { httpsAgent: agent });
}

// Good: reusing the agent
const agent = new HttpsProxyAgent(proxyUrl);
const axiosInstance = axios.create({
  httpsAgent: agent,
  httpAgent: agent
});

async function goodExample(url) {
  return axiosInstance.get(url);
}

2. Timeout Configuration

Always set reasonable timeouts to prevent the application from hanging:

const axiosInstance = axios.create({
  httpsAgent: agent,
  timeout: 10000, // General timeout
  // Detailed timeout settings for Got
  // timeout: {
  //   lookup: 1000,    // DNS lookup
  //   connect: 2000,   // Connecting to the proxy
  //   secureConnect: 2000, // SSL handshake
  //   socket: 5000,    // Socket inactivity
  //   response: 3000,  // Waiting for the first byte of response
  //   send: 10000,     // Sending the request
  //   request: 15000   // Entire request
  // }
});

3. Concurrency Control

Limit the number of simultaneous requests to prevent overload:

const pLimit = require('p-limit');

// Limiting to 5 simultaneous requests
const limit = pLimit(5);

async function processUrls(urls) {
  const promises = urls.map(url => 
    limit(() => axiosInstance.get(url))
  );
  
  return Promise.allSettled(promises);
}

4. User-Agent Rotation

Combine proxy rotation with User-Agent rotation for greater anonymity:

const userAgents = [
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
  'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
  'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0'
];

function getRandomUserAgent() {
  return userAgents[Math.floor(Math.random() * userAgents.length)];
}

async function fetchWithRandomUA(url) {
  return axiosInstance.get(url, {
    headers: {
      'User-Agent': getRandomUserAgent()
    }
  });
}

5. Monitoring and Logging

Implement a monitoring system to track proxy performance:

class ProxyMonitor {
  constructor() {
    this.stats = new Map();
  }
  
  recordRequest(proxyUrl, success, responseTime) {
    if (!this.stats.has(proxyUrl)) {
      this.stats.set(proxyUrl, {
        total: 0,
        success: 0,
        failed: 0,
        totalResponseTime: 0,
        avgResponseTime: 0
      });
    }
    
    const stat = this.stats.get(proxyUrl);
    stat.total++;
    
    if (success) {
      stat.success++;
      stat.totalResponseTime += responseTime;
      stat.avgResponseTime = stat.totalResponseTime / stat.success;
    } else {
      stat.failed++;
    }
  }
  
  getReport() {
    const report = [];
    
    this.stats.forEach((stat, proxyUrl) => {
      report.push({
        proxy: proxyUrl.replace(/\/\/.*@/, '//***@'),
        successRate: ((stat.success / stat.total) * 100).toFixed(2) + '%',
        avgResponseTime: stat.avgResponseTime.toFixed(0) + 'ms',
        total: stat.total,
        success: stat.success,
        failed: stat.failed
      });
    });
    
    return report;
  }
}

const monitor = new ProxyMonitor();

// Wrapper for requests with monitoring
async function monitoredRequest(url, proxyUrl) {
  const startTime = Date.now();
  
  try {
    const response = await axiosInstance.get(url);
    const responseTime = Date.now() - startTime;
    monitor.recordRequest(proxyUrl, true, responseTime);
    return response.data;
  } catch (error) {
    const responseTime = Date.now() - startTime;
    monitor.recordRequest(proxyUrl, false, responseTime);
    throw error;
  }
}

// Periodic report
setInterval(() => {
  console.table(monitor.getReport());
}, 60000);

6. Choosing the Type of Proxy

Choose the type of proxy based on the task:

Task Recommended Type Reason
Mass scraping of public data Datacenter proxies High speed, low cost
Working with social networks and protected platforms Residential proxies Real IPs, low risk of blocking
Emulating mobile traffic Mobile proxies IP addresses of mobile operators
Working with non-standard protocols SOCKS5 proxies Support for any traffic

For tasks requiring a high level of anonymity and minimal risk of blocking, it is recommended to use residential proxies. For high-speed scraping of large volumes of data, datacenter proxies are suitable.

7. Security of Credentials

Never store proxy credentials in code. Use environment variables:

// .env file
PROXY_HOST=proxy-server.com
PROXY_PORT=8080
PROXY_USERNAME=your-username
PROXY_PASSWORD=your-password

// In code
require('dotenv').config();

const proxyUrl = `http://${process.env.PROXY_USERNAME}:${process.env.PROXY_PASSWORD}@${process.env.PROXY_HOST}:${process.env.PROXY_PORT}`;

// Or for a list of proxies
const proxyList = process.env.PROXY_LIST.split(',').map(proxy => proxy.trim());

Conclusion

Setting up proxies in Node.js applications is an important skill for developers working with scraping, automation, and integration with external APIs. In this guide, we have covered all the main ways to work with proxies: from basic setup using built-in modules to advanced techniques with rotation, error handling, and monitoring.

Key points to remember:

  • Choose an HTTP request library based on the task: Axios for versatility, Got for TypeScript projects, node-fetch for compatibility with browser API
  • Use SOCKS5 proxies for working with non-standard protocols and maximum flexibility
  • Implement proxy rotation for load distribution and to avoid bans
  • Implement an error handling system with automatic retries and proxy switching
  • Monitor proxy performance and automatically exclude non-working ones
  • Reuse HTTP agents and set reasonable timeouts
  • Store credentials in environment variables, not in code

When choosing proxies for your Node.js applications, consider the specifics of the task. For scraping protected platforms and working with APIs sensitive to IP reputation, we recommend using residential proxiesβ€”they provide a high level of anonymity and minimal risk of blocking due to the use of real IP addresses of home users.