Vumela i-node-fetch ikhethe i-proxy agent ngemuva kokudluliswa
Sebenzisa umsebenzi we-agent ezindaweni ze-HTTP ne-HTTPS ukuze ukudluliswa kukwazi ukukhetha i-proxy agent efanele.
I-URL yokuqala ingaba yi-HTTPS kanti i-URL edlulisiwe ingaba yi-HTTP. Khetha i-agent ngokwendawo yesicelo ngasinye, esikhundleni sokubopha i-agent eyodwa ku-URL yokuqala.
Ngephakethe le-node-fetch elingenisiwe kuhlelo lwe-ESM, nikeza umsebenzi:
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,
});Shintsha i-URL ye-API nendawo yokugcina ye-proxy. Nge-API yomphakathi evunyelwe ngesixhumanisi somphakathi, uhlu lwe-proxy ye-HTTP lunikeza abantu abangaba yikhandidethi ye-proxyUrl. Womabili ama-agent angasebenzisa isilaleli se-proxy ye-HTTP esisodwa; amagama awo akhomba isicelo esiya endaweni.
| I-URL enikezwe isikhethi | I-agent ekhethiwe |
|---|---|
| https://… | httpsAgent |
| http://… | httpAgent |
Ngakho-ke umsebenzi usakhetha i-agent ehambisanayo uma ukudluliswa kushintsha iphrothokholi yendawo. I-proxyUrl isaqhubeka ichaza isixhumanisi, ngaphandle kwalokho okunqumayo.
Dala ama-agent kanye ngomthwalo womsebenzi bese uwasebenzisa futhi. Phatha isimo esibuyisiwe futhi ufunde umzimba wempendulo kukhodi ebizayo. Lesi sikhombisi se-agent singesika-node-fetch; i-fetch eyakhelwe ngaphakathi ku-Node isebenzisa esinye isikhombisi sokuthutha.