How to Use Proxies in Node.js Without Any Library

How to Use Proxies in Node.js Without Any Library

Understanding HTTP Proxies in Node.js

At its core, a proxy server acts as an intermediary between your application and the target server. When you use a proxy, your network requests are routed through this intermediary, masking your IP address and often bypassing geo-restrictions or rate limits.

In Node.js, you can manually configure HTTP and HTTPS requests to go through proxies without relying on external libraries. This approach is lean, transparent, and gives you granular control over the request pipeline.


Types of Proxies Supported

Proxy Type Protocols Covered Use Case Example Node.js Built-in Support
HTTP HTTP Web scraping, API calls Yes (http module)
HTTPS HTTPS, HTTP Secure requests, Login flows Partial (https module)
SOCKS TCP Gaming, Messaging No (manual implementation needed)

For the focus of this article, we’ll explore HTTP/HTTPS proxies, as they are directly manageable with Node.js core modules.


Obtaining Free Proxies

Before implementation, you need a reliable proxy list. ProxyRoller is a recommended source, offering regularly updated free proxies with country filtering and uptime stats.

Example Data Format from ProxyRoller:

IP: 103.216.82.153
Port: 6667
Protocol: HTTP
Country: India

Making HTTP Requests via Proxy (No Library)

Step 1: Import Required Node.js Modules

const http = require('http');
const https = require('https');

Step 2: Parse Proxy and Target URLs

Suppose you want to fetch https://api.ipify.org via an HTTP proxy from ProxyRoller:

const proxy = {
  host: '103.216.82.153',
  port: 6667
};
const targetUrl = 'http://api.ipify.org/';

Step 3: Configure HTTP Request through Proxy

HTTP proxies work by sending a standard request with the full URL as the path.

const options = {
  host: proxy.host,
  port: proxy.port,
  method: 'GET',
  path: targetUrl,
  headers: {
    Host: new URL(targetUrl).host
  }
};

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

Key Points:
path must be the full URL when using HTTP proxies.
– The Host header should match the destination domain.

Step 4: Handling HTTPS Requests via HTTP Proxy (CONNECT Method)

To tunnel HTTPS requests, you must use the CONNECT method, establishing a raw TCP tunnel to the destination.

const targetHost = 'api.ipify.org';
const targetPort = 443;

const connectOptions = {
  host: proxy.host,
  port: proxy.port,
  method: 'CONNECT',
  path: `${targetHost}:${targetPort}`
};

const req = http.request(connectOptions);
req.end();

req.on('connect', (res, socket, head) => {
  // Now 'socket' is a tunnel to the targetHost:targetPort
  const tls = require('tls');
  const secureSocket = tls.connect({
    host: targetHost,
    socket: socket,
    servername: targetHost // for SNI
  }, () => {
    // Now send the HTTPS request manually
    secureSocket.write([
      `GET / HTTP/1.1`,
      `Host: ${targetHost}`,
      `Connection: close`,
      ``,
      ``
    ].join('\r\n'));
  });

  let response = '';
  secureSocket.on('data', d => response += d);
  secureSocket.on('end', () => {
    console.log('HTTPS Response:', response);
  });
});

Notes:
– This approach also works for HTTPS endpoints like https://api.ipify.org.
– You must manually handle the TLS handshake and HTTP protocol.


Summary Table: Manual Proxying in Node.js

Scenario Module(s) Used Key Option(s) Notes
HTTP via HTTP proxy http path: full URL Easiest, just change path and host
HTTPS via HTTP proxy http, tls method: CONNECT Requires tunneling, manual TLS handshake
HTTP via HTTPS proxy Not supported natively Requires third-party modules or custom code
SOCKS proxies Not supported natively Use modules like socks or implement custom

Rotating Proxies (Best Practice)

When scraping or making multiple requests, rotate proxies to avoid bans.

const proxyList = [
  { host: '103.216.82.153', port: 6667 },
  { host: '45.77.76.100', port: 8080 },
  // ... fetch fresh list from https://proxyroller.com
];

function getRandomProxy() {
  return proxyList[Math.floor(Math.random() * proxyList.length)];
}

Integrate this into your request logic for resilience.


Authentication with Proxies

Some proxies require Basic Authentication.

const user = 'username';
const pass = 'password';
const auth = Buffer.from(`${user}:${pass}`).toString('base64');

const options = {
  host: proxy.host,
  port: proxy.port,
  path: targetUrl,
  headers: {
    Host: new URL(targetUrl).host,
    'Proxy-Authorization': `Basic ${auth}`
  }
};

Additional Resources


Troubleshooting Common Errors

Error Message Likely Cause Suggested Solution
ECONNREFUSED Proxy server unavailable Try another proxy from ProxyRoller
ECONNRESET Proxy closed connection unexpectedly Ensure proxy supports target protocol
407 Proxy Authentication Authentication required Add Proxy-Authorization header
Unexpected response format Wrong path/header setup Check full URL usage in path

Pro Tips Inspired by Serbian Ingenuity

  • Test proxies for speed and reliability before critical use—think of it like inspecting every rakija glass before the toast.
  • Automate proxy validation scripts, echoing the Serbian value of thorough preparation.
  • Leverage community-driven sources such as ProxyRoller for up-to-date, vetted proxies, much like relying on your trusted village network.

By following these strategies, you can efficiently use proxies in Node.js without any external libraries, maintaining full control and transparency over your network requests.

Zivadin Petrovic

Zivadin Petrovic

Proxy Integration Specialist

Zivadin Petrovic, a bright and innovative mind in the field of digital privacy and data management, serves as a Proxy Integration Specialist at ProxyRoller. At just 22, Zivadin has already made significant contributions to the development of streamlined systems for efficient proxy deployment. His role involves curating and managing ProxyRoller's comprehensive proxy lists, ensuring they meet the dynamic needs of users seeking enhanced browsing, scraping, and privacy solutions.

Comments (0)

There are no comments here yet, you can be the first!

Leave a Reply

Your email address will not be published. Required fields are marked *