Een proxy instellen in Node.js (Axios & native fetch)
Updated:
De ingebouwde optie proxy: { host, port } van Axios mislukt bij https://-doelen omdat deze geen HTTP CONNECT-tunnel opzet. Om Node.js- en TypeScript-verzoeken probleemloos via geauthenticeerde HTTP(S)-proxy's van VinaProxy te routeren, geef je een HttpsProxyAgent (https-proxy-agent) door aan Axios of een undici.ProxyAgent-dispatcher aan de native fetch() van Node.
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
Proxyconfiguratie: Axios vs. native Node.js fetch()
Beide bibliotheken accepteren standaard http://user:pass@ip:port-URL's uit je VinaProxy-dashboard:
| HTTP-client | Pakket | Aanbevolen codefragment |
|---|---|---|
| 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' }) } }) |
Best practices voor connection pooling & timeouts in Node.js
- Stel in Axios altijd
proxy: falsein naasthttpsAgent, zodat Axios conflicterendeHTTP_PROXY-omgevingsvariabelen van het systeem negeert - Initialiseer
HttpsProxyAgentofProxyAgentéén keer per proxy-IP buiten je lus om persistente keep-alive-sockets te hergebruiken - Exporteer je VinaProxy-proxylijst direct als JSON-array met behulp van onze Proxy Format Converter
Stappenplan
- 1
Installeer https-proxy-agent (of undici)
Voer
npm install axios https-proxy-agentuit in je Node.js-project. - 2
Maak de HttpsProxyAgent-instantie aan
Initialiseer
const agent = new HttpsProxyAgent('http://user:pass@ip:port');met behulp van je VinaProxy-inloggegevens. - 3
Geef httpsAgent en proxy: false door aan Axios
Roep
const res = await axios.get('https://vinaproxy.com/api/my-ip', { httpsAgent: agent, proxy: false, timeout: 10000 });aan. - 4
Verifieer het uitgaande IP-adres
Log
res.dataom te bevestigen dat het antwoord via je dedicated proxy-IP is binnengekomen.
Veelgestelde vragen
Waarom mislukt de standaard `proxy: { host, port, auth }` van Axios bij HTTPS-URL's?
De ingebouwde proxyconfiguratie van Axios verzendt een standaard HTTP-verzoek in plaats van een HTTP CONNECT TLS-tunnel te openen. Dit los je op door httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port') met proxy: false te gebruiken.
Hoe gebruik ik een proxy met native `fetch()` in Node.js 18/20/22?
Importeer ProxyAgent uit undici en geef { dispatcher: new ProxyAgent('http://user:pass@ip:port') } door in het optie-object van fetch().
Kan ik mijn proxylijst converteren naar JSON voor Node.js?
Ja, plak je ip:port:user:pass-regels in onze gratis Proxy Format Converter en selecteer JSON-uitvoer.
Versterk je Node.js-backends & scrapers met statische IP's
Direct onbeperkte Dedicated IPv4- en Statische ISP-proxy's in 7 landen.
Koop Node.js-proxy's