Hoe om 'n proxy in Node.js op te stel (Axios & inheemse Fetch)
Updated:
Axios se ingeboude opsie proxy: { host, port } misluk op https://-teikens omdat dit nie 'n HTTP CONNECT-tonnel skep nie. Om Node.js- en TypeScript-versoeke skoon deur geverifieerde VinaProxy-HTTP(S)-proxies te roeteer, gee 'n HttpsProxyAgent (https-proxy-agent) aan Axios deur of 'n undici.ProxyAgent-verdeler aan inheemse Node-fetch().
new HttpsProxyAgent('http://user:pass@ip:port')- 1 IP₫40 000
- 5+ IP's₫35 000
- 10+ IP's₫25 000
- 20+ IP's₫25 000
- 50+ IP's₫25 000
- 100+ IP's₫25 000
- 200+ IP's₫25 000
- 500+ IP's₫25 000
- 1000+ IP's₫22 000
- 2000+ IP's₫20 000
- 1 IP₫40 000
- 5+ IP's₫35 000
- 10+ IP's₫25 000
- 20+ IP's₫25 000
- 50+ IP's₫25 000
- 100+ IP's₫25 000
- 200+ IP's₫25 000
- 500+ IP's₫25 000
- 1000+ IP's₫22 000
- 2000+ IP's₫20 000
Axios teenoor inheemse Node.js fetch()-proxy-konfigurasie
Albei biblioteke aanvaar standaard http://user:pass@ip:port-URL'e vanaf jou VinaProxy-kontroleskerm:
| HTTP-kliënt | Pakket | Aanbevole kodebrokkie |
|---|---|---|
| Axios | npm i https-proxy-agent | axios.get(url, { httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port'), proxy: false }) |
Native fetch (Node 18+) | npm i undici | fetch(url, { dispatcher: new ProxyAgent('http://user:pass@ip:port') }) |
| Got | npm i hpagent | got(url, { agent: { https: new HttpsProxyAgent({ proxy: 'http://user:pass@ip:port' }) } }) |
Beste praktyke vir verbindingspoele en uitteltye in Node.js
- Stel altyd
proxy: falsesaam methttpsAgentin Axios in sodat Axios botsende stelsel-omgewingsveranderlikes virHTTP_PROXYignoreer - Inisialiseer
HttpsProxyAgentofProxyAgenteen keer per proxy-IP buite jou lus om volgehoue keep-alive-sokke te hergebruik - Voer jou VinaProxy-proxy-lys direk uit as 'n JSON-skikking met ons Proxy Format Converter
Stap vir stap
- 1
Installeer https-proxy-agent (of undici)
Voer
npm install axios https-proxy-agentin jou Node.js-projek uit. - 2
Skep die HttpsProxyAgent-instansie
Inisialiseer
const agent = new HttpsProxyAgent('http://user:pass@ip:port');met jou VinaProxy-aanmeldbesonderhede. - 3
Gee httpsAgent en proxy: false aan Axios deur
Roep
const res = await axios.get('https://vinaproxy.com/api/my-ip', { httpsAgent: agent, proxy: false, timeout: 10000 });op. - 4
Verifieer die uitgangs-IP
Teken
res.dataaan om te bevestig dat die antwoord deur jou toegewyde proxy-IP gekom het.
Gereelde vrae
Waarom misluk Axios se verstek `proxy: { host, port, auth }` op HTTPS-URL'e?
Axios se ingeboude proxy-konfigurasie stuur 'n gewone HTTP-versoek in plaas daarvan om 'n HTTP CONNECT-TLS-tonnel oop te maak. Die gebruik van httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port') met proxy: false los dit op.
Hoe gebruik ek 'n proxy met inheemse `fetch()` in Node.js 18/20/22?
Voer ProxyAgent vanaf undici in en gee { dispatcher: new ProxyAgent('http://user:pass@ip:port') } in die fetch()-opsie-objek deur.
Kan ek my proxy-lys na JSON vir Node.js omskakel?
Ja—plak jou ip:port:user:pass-reëls in ons gratis Proxy Format Converter en kies JSON-afvoer.
Dryf jou Node.js-agterstelsels en skrapers aan met statiese IP's
Onmiddellike, onbeperkte Toegewyde IPv4- en Statiese ISP-proxies oor 7 lande heen.
Koop Node.js-proxies