Kā iestatīt starpniekserveri Node.js vidē (Axios un Native Fetch)
Updated:
Axios iebūvētā opcija proxy: { host, port } nedarbojas ar https:// mērķiem, jo tā neizveido HTTP CONNECT tuneli. Lai netraucēti maršrutētu Node.js un TypeScript pieprasījumus caur VinaProxy autentificētiem HTTP(S) starpniekserveriem, nododiet HttpsProxyAgent (https-proxy-agent) klientam Axios vai undici.ProxyAgent dispečeru iebūvētajai Node fetch() funkcijai.
new HttpsProxyAgent('http://user:pass@ip:port')- 1 IP40 000 ₫
- 5+ IP35 000 ₫
- 10+ IP25 000 ₫
- 20+ IP25 000 ₫
- 50+ IP25 000 ₫
- 100+ IP25 000 ₫
- 200+ IP25 000 ₫
- 500+ IP25 000 ₫
- 1000+ IP22 000 ₫
- 2000+ IP20 000 ₫
- 1 IP40 000 ₫
- 5+ IP35 000 ₫
- 10+ IP25 000 ₫
- 20+ IP25 000 ₫
- 50+ IP25 000 ₫
- 100+ IP25 000 ₫
- 200+ IP25 000 ₫
- 500+ IP25 000 ₫
- 1000+ IP22 000 ₫
- 2000+ IP20 000 ₫
Axios pret iebūvēto Node.js fetch() starpniekservera konfigurāciju
Abas bibliotēkas atbalsta standarta http://user:pass@ip:port URL no jūsu VinaProxy vadības paneļa:
| HTTP klients | Pakotne | Ieteicamais koda fragments |
|---|---|---|
| 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' }) } }) |
Savienojumu pūla un noildzes labākā prakse Node.js vidē
- Vienmēr iestatiet
proxy: falselīdzāshttpsAgentklientā Axios, lai tas ignorētu konfliktējošos sistēmasHTTP_PROXYvides mainīgos - Inicializējiet
HttpsProxyAgentvaiProxyAgentvienu reizi katrai starpniekservera IP ārpus cikla, lai atkārtoti izmantotu pastāvīgos keep-alive ligzdu savienojumus - Eksportējiet savu VinaProxy starpniekserveru sarakstu tieši kā JSON masīvu, izmantojot mūsu Proxy Format Converter
Soli pa solim
- 1
Instalējiet https-proxy-agent (vai undici)
Palaidiet
npm install axios https-proxy-agentsavā Node.js projektā. - 2
Izveidojiet HttpsProxyAgent instanci
Inicializējiet
const agent = new HttpsProxyAgent('http://user:pass@ip:port');, izmantojot savus VinaProxy akreditācijas datus. - 3
Nododiet httpsAgent un proxy: false klientam Axios
Izsauciet
const res = await axios.get('https://vinaproxy.com/api/my-ip', { httpsAgent: agent, proxy: false, timeout: 10000 });. - 4
Pārbaudiet izejas IP adresi
Reģistrējiet
res.data, lai apstiprinātu, ka atbilde saņemta caur jūsu dedicēto starpniekservera IP adresi.
Biežāk uzdotie jautājumi
Kāpēc Axios noklusējuma `proxy: { host, port, auth }` nedarbojas ar HTTPS URL?
Axios iebūvētā starpniekservera konfigurācija nosūta parastu HTTP pieprasījumu, nevis atver HTTP CONNECT TLS tuneli. To atrisina httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port') izmantošana kopā ar proxy: false.
Kā izmantot starpniekserveri ar iebūvēto `fetch()` Node.js 18/20/22 versijās?
Importējiet ProxyAgent no undici un nododiet { dispatcher: new ProxyAgent('http://user:pass@ip:port') } funkcijas fetch() opciju objektā.
Vai es varu konvertēt savu starpniekserveru sarakstu uz JSON priekš Node.js?
Jā — iekopējiet savas ip:port:user:pass rindiņas mūsu bezmaksas rīkā Proxy Format Converter un izvēlieties JSON izvadi.
Nodrošiniet savas Node.js aizmugursistēmas ar statiskām IP
Tūlītēji neierobežoti Dedicated IPv4 un Static ISP starpniekserveri 7 valstīs.
Pirkt Node.js starpniekserverus