How to use proxies with NodeJS axios?

Javascript's axios is a popular HTTP client often used when web scraping with nodejs.
To use proxies with axios we can use the proxy parameter in get and postj methods:

const axios = require('axios');
axios.get('https://httpbin.dev/ip', {
  proxy: {
    host: "160.11.12.13",
    port: "8080",
    auth: {
      username: "username",
      password: "password",
    }
  }
})
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error(error);
});

Note that axios does not support automatic proxy use through terminal environment variables HTTP_PROXY, HTTPS_PROXY or ALL_PROXY.

Axios also does not support SOCKS proxies directly but it can be enabled through socks-proxy-agent library:

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

const proxy = 'socks5://localhost:1080';

const httpAgent = new SocksProxyAgent(proxy);
const httpsAgent = new SocksProxyAgent(proxy);

axios.get('http://example.com', {
  httpAgent,
  httpsAgent,
})
.then((response) => {
  console.log(response.data);
})
.catch((error) => {
  console.error(error);
});

Finally, when web scraping, it's best to rotate proxies for each request. For that see our article: How to Rotate Proxies in Web Scraping

Question tagged: NodeJS, HTTP

Related Posts

How to Scrape Sitemaps to Discover Scraping Targets

Usually to find scrape targets we look at site search or category pages but there's a better way - sitemaps! In this tutorial, we'll be taking a look at how to find and scrape sitemaps for target locations.

Web Scraping With Node-Unblocker

Tutorial on using Node-Unblocker - a nodejs library - to avoid blocking while web scraping and using it to optimize web scraping stacks.

Web Scraping With NodeJS and Javascript

In this article we'll take a look at scraping using Javascript through NodeJS. We'll cover common web scraping libraries, frequently encountered challenges and wrap everything up by scraping etsy.com