Introduction
Axios is a popular HTTP client for browsers and node.js. Many developers love to use it because it offers more features than the more common Fetch API. One feature that makes Axios stand out is the HTTP interceptors, which allow you to alter all requests sent from your application from any part of the world. Â
A common application of Axios is web scraping, as it allows developers to download website content and parse it with a library such as Cheerio. With a few lines of code, you can use Axios (a powerful JavaScript library) to send GET requests to a target website, retrieve its HTML content, and store the data in various formats, such as CSV or JSON. Subsequently, when you use Axios to connect to a website, your IP address is exposed. As a result, the website can block or ban your IP if automated activities are detected.Â
However, you can avoid this unwanted situation by using a proxy, which acts as a barrier between the client and server. They work by hiding your online activities and protecting the integrity of your IP address. Apart from masking your IP addresses, proxies play several other crucial roles.
Therefore, this guide will examine how to set up proxies on Axios as well as other FAQs.
Setting up a proxy with Axios
In this section, we shall examine the step-by-step guide required to set up proxies with Axios.
Set up dependencies
The first step in configuring a proxy on Axios is to set up dependencies. It involves creating a folder on your computer to store the code and to initialize a new Node.js project:
$ mkdir nodeaxios
$ cd nodeaxios
$ npm init –y
The next step is to install the Axios dependency and create the app.js file where you will store the project code. This is shown below:
$ npm i axios
$ touch app.js
Once you run the above code, you should get some new files and folders in the nodeaxios directory. Now, you can create a start command by editing the package.json file as shown below:
{
 “name”: “nodeaxios”,
 “version”: “1.0.0”,
 “description”: “”,
 “main”: “index.js”,
 “scripts”: {
   “start”: “node app.js “
 },
 “keywords”: [],
 “author”: “”,
 “license”: “ISC”,
 “dependencies”: {
   “axios”: “^1.1.3”
 }
}
At this stage, the npm start function should run without returning any error codes if your setup is correct:
$ npm start
> nodeaxios@1.0.0 start
> node app.js
Make a basic HTTP request
With the codes below, you can make basic HTTP requests via Axios:
const axios = require(‘axios’);
axios.get(‘https://api.ipify.org/?format=json’)
   .then(res => {
       console.log(res.data)
   }).catch(err => console.error(err))
The next step is to save the code in app.js and run the npm.start function. Subsequently, it will print your public IP address if the code is working as intended:
$ npm start
> nodeaxios@1.0.0 start
> node app.js
{ ip: ‘212.12.101.110’ }
Integrate a proxy with Axios
You can introduce a proxy in sending a GET request with Axios like this:
axios.get(‘https://api.ipify.org/?format=json’,
   {
       proxy: {
           protocol: ‘http’,
           host: ‘212.12.101.110’,
           port: 8080
       }
   }
)
   .then(res => {
       console.log(res.data)
   }).catch(err => console.error(err))
Bear in mind that the proxy used above is from a free provider. In the subsequent step, we shall examine how to integrate a premium proxy that requires authentication. The result of the above code will be printed in the terminal like this:
$ npm start
> nodeaxios@1.0.0 start
> node app.js
212.12.101.110
Integrating a premium proxy with Axios
Free proxies often come with several security risks. Therefore, if you want proxies that offer the highest level of data security, privacy, or anonymity, you need to consider premium proxies. One characteristic of premium proxies is that they often require authentication details like username and password.Â
Axios supports the auth property that passes the defined username and password to the proxy:
axios.get(‘https://api.ipify.org/?format=json’,
   {
       proxy: {
           protocol: ‘http’,
           host: ‘129.119.39.101’,
           port: 5959,
           auth: {
               username: ‘ticketing1’,
               password: ‘p@$$w0Rd’
           }
       }
   }
)
   .then(res => {
       console.log(res.data)
   }).catch(err => console.error(err))
Replace the proxy details with those provided by your proxy provider. Once that is done, you can run the code, and you should see a result like this:
$ npm start
> nodeaxios@1.0.0 start
> node app.js
129.119.39.101
Configure a proxy with environment variables
Another method of configuring a proxy via Axios is to set up the http_proxy or https_proxy environment variables. This method allows you to bypass the proxy setup within Axios commands. Subsequently, Axios will automatically use the proxy details as provided by the environment variables.Â
You can simply update the code in app.js and change the axios request to the default format as shown below:
const axios = require(‘axios’);
console.log(process.env.http_proxy);
axios.get(‘https://api.ipify.org/?format=json’)
   .then(res => {
       console.log(res.data)
   }).catch(err => console.error(err))
The next step is to set the https_proxy environment variable and run the app.js file again, as shown below:
$ export https_proxy=https:// 212.12.101.110:8080/Â
$ npm start
> nodeaxios@1.0.0 start
> node app.js
212.12.101.110
The above code is illustrated with a free proxy. However, using a proxy that requires authentication follows the same format except for the proxy detail settings. Here is how you are supposed to write the proxy authentication detail in the code above:
export https_proxy=https://username:password@host:port/
Using rotating proxies to send requests on Axios
Although a proxy masks your IP address, it can still be blocked if the website detects bot activity. Therefore, for activities like web scraping, you may need to rotate proxies so that each request is sent with a random IP address.
const axios = require(‘axios’);
// An array of 5 proxies
const proxy_list = [
   {
       protocol: ‘https’,
       host: ‘180.183.157.159’,
       port: 80,
   },
   //…
   {
       protocol: ‘socks5’,
       host: ‘ 164.68.98.169,
       port: 9300,
   },
   {
       protocol: ‘http’,
       host: ‘154.129.98.156’,
       port: 8080,
   },
   {
       protocol: ‘http’,
       host: ‘ 91.211.245.176’,
       port: 8080,
   },
   {
       protocol: ‘http’,
       host: ‘164.129.114.111’,
       port: 8080,
   }
]
let random_index = Math.floor(Math.random() * proxy_list.length);
axios.get(‘https://api.ipify.org/?format=json’,
   {
       proxy: proxy_list[random_index]
   }
)
   .then(res => {
       console.log(res.data)
   }).catch(err => console.error(err))
Now, you can modify the code to meet your specific needs. With each session, Axios will use a random proxy from the proxy_list.
How to Use NetNut Proxies
Choosing the best proxy involves considering factors like speed, coverage, reliability, and cost. The primary purpose of using a proxy is for security, anonymity, and privacy. We examined the use of free proxies with Axios but they are largely unreliable. Therefore, you need to use a reliable and reputable proxy provider like NetNut.
NetNut is an industry-leading provider with an extensive network of over 85 million rotating residential proxies in 195 countries and over 1 million mobile IPS in over 100 countries.Â
You can opt for static residential proxies if you need to maintain the same IP for a particular session. However, for web scraping, you need rotating residential proxies. In addition, NetNut offers automated IP rotation to ensure the highest level of anonymity.Â
NetNut provides you with different proxy solutions ranging from ISP proxies to static residential proxies, to US residential proxies. Static residential proxies allow you to maintain one IP address for a certain session. ISP proxies, on the other hand, come in handy to bypass captcha and geo-restrictions.
Another unique feature of NetNut is it offers excellent customer support services. You can always get prompt support via email or live chat on the website.Â
Conclusion
This article has examined how to configure a proxy with Axios. We first started by installing dependencies and then configured a free proxy on Axios. In addition, we examined how to use a premium proxy that requires authentication.
Axios is a powerful tool for making HTTP requests in Node.js. This library stands out for its simplicity, flexibility, and support for many features. One of the applications of Axios is web scraping, and IP block is a common limitation. Therefore, using a proxy is an excellent option for large-scale web scraping.Â
If you need a reliable proxy provider, then you should consider using NetNut proxies. Feel free to contact us if you need expert guidance for your proxy needs.Â
Frequently Asked Questions
What are the factors to consider for proxy testing?
The first factor to consider when testing proxies is speed. A proxy that is too slow can result in delayed requests, failed connection, or request timeouts. When you use proxy testing tools, you can get an overview of the load times and how they work to meet your demands.
Another factor to consider is the security offered by the proxy server. This is a crucial factor since one of the primary reasons for using proxies is security. Various tools can test the security of a proxy’s SSL certificate and provide a security score.
Reliability is another factor that determines your choice of proxy. If your proxy server disconnects when you need to connect to a server, it indicates that it is unreliable.Â
How can I troubleshoot common proxy challenges?
Here are some common challenges regarding proxies and how to solve them:
- IP bans: If your IP address is getting banned frequently, it may be an indication that the proxy server is unreliable. Therefore, you may need to choose a reliable proxy provider.
- Proxies not connecting: Another problem associated with proxy configuration is error response. This may be an indication that the data is not properly transmitting or there is a problem with the proxy provider. Therefore, you may need to consult your proxy provider.
- Incorrect configuration: When your proxy is not correctly configured, you may get an error response. Therefore, you may need to contact the proxy provider to get details on the appropriate steps to configure the proxy on Axios.
Why should I use proxies?
Here are some reasons why developers choose Axios:
- Axios has a simple API, which makes it easy to perform HTTP requests.
- This library is built on Promises, which makes it easier to work with asynchronous operations.
- It eliminates the need to manually parse JSON as it automatically transforms the web content when making requests and receiving responses.Â
- Axios supports request and response interceptors, which allows it to pre-process or post-process requests and responses.