The first URL may be HTTPS and a redirected URL HTTP. Choose the agent for the destination of each request, rather than keeping one agent tied to the first URL.

For the imported node-fetch package in an ESM application, supply a function:

import fetch from 'node-fetch';
import { HttpProxyAgent } from 'http-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';

const proxyUrl = 'http://proxy.example:8080';
const httpAgent = new HttpProxyAgent(proxyUrl);
const httpsAgent = new HttpsProxyAgent(proxyUrl);

const response = await fetch('https://api.example/', {
  agent: url => url.protocol === 'http:' ? httpAgent : httpsAgent,
});

Replace the API URL and proxy endpoint. For a public API allowed through a public intermediary, the HTTP proxy list supplies candidates for proxyUrl. The two agents can use the same HTTP proxy listener; their names refer to the destination request.

URL passed to the selectorSelected agent
https://…httpsAgent
http://…httpAgent

The function therefore still selects the matching agent if a redirect changes the destination protocol. proxyUrl continues to describe the intermediary, independently of that decision.

Create the agents once for the workload and reuse them. Handle the returned status and consume the response body in the calling code. This agent interface belongs to node-fetch; Node's built-in fetch uses a different transport interface.

Аффингæй хæстæг статьялтæ