Node-fetch is a popular package that brings window.fetch functionality to NodeJS code.
window.fetch is a client-side API for making async HTTP requests in web browsers. Using this API, the developers can fetch JSON files, images, or any other data from the server.
Node Fetch brings the same functionality to the server-side applications using Node.JS. With this package, developers can send HTTP GET and POST requests. It also allows setting headers, credentials, and many other options that make it perfect for web scraping.
This guide will show you how to integrate Oxylabs Residential and Datacenter Proxies with Node-Fetch.
The first step is installing node.js if you don't have it. Head over to the downloads page of Nodejs.org and download Node.
The next step is to install the node-fetch package. To install this package, we can use the Node Package Manager tool.
Important note: Node Fetch from version 3 is an ESM-only module. In this tutorial, we will be using version 2 so that it remains compatible with CommonJS.
Open the terminal and navigate to the directory where you want to keep your source code. After that, run the following command to install node-fetch:
npm install node-fetch@2
Create a new file and save it as check-ip.js.
Then, load the node-fetch module. To load the module, add the following line of code:
const fetch = require("node-fetch");
You can now use either the then-catch syntax as follows:
fetch('https://ip.oxylabs.io')
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
Alternatively, you can use the newer try-catch syntax as follows:
(async () => {
try {
const response = await fetch("https://ip.oxylabs.io");
const data = await response.text();
console.log(data);
} catch (e) {
console.error(e.message);
}
})();
Save this file and open the terminal. Enter the following command to run it:
node check-ip.js
The output is your IP address.
To use proxies, we first need to load the https-proxy-agent package.
Create a new file and save it as proxies.js. Add the following lines to load both the required packages:
const fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');
Next, we must create a variable for the http or https proxy URL.
Most proxy servers, such as Oxylabs' Residential and Datacenter proxy servers, require you to send the credentials. In such cases, you can construct the http proxy user as follows:
const proxyUrl = `http://${username}:${password}@${proxyServer}`;
We are using three other local variables to create the proxy URL here. These local variables store your username, password, and proxy server.
The following example shows how the https proxy or the http proxy URL would be for Oxylabs Residential Proxies:
const proxyUrl = `http://<USERNAME>:<PASSWORD>@pr.oxylabs.io:7777`;
Here, USERNAME and PASSWORD are your Oxylabs sub-user's credentials.
Here, you can use country-specific entries. For example, if you use the proxy server as au-pr.oxylabs.io:40000 instead of pr.oxylabs.io:7777, you'll get an IP in Australia.
Please see our documentation for a complete list of country-specific entry nodes and sticky session details.
The following table summarizes the proxy server configuration for Shared and Dedicated Datacenter Proxies.
Datacenter Proxies | Proxy Type | Proxy Address | Port | User Credentials |
---|---|---|---|---|
Dedicated | HTTP or SOCKS5 | Your selected IP from the acquired list | 60000 | Oxylabs sub-user’s username and password |
Shared | HTTP | dc.pr.oxylabs.io | 10000 | Oxylabs sub-user’s username and password |