내장 fetch 및 undici#
Node의 글로벌 fetch undici입니다. 그것을 주십시요 ProxyAgent 요청당 dispatcher 옵션, 또는 한 번 글로벌 설정 setGlobalDispatcher.
import { fetch, ProxyAgent, setGlobalDispatcher } from "undici";
const proxy = new ProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
// Per request
const res = await fetch("https://api.ipify.org", { dispatcher: proxy });
console.log(await res.text());
// Or globally, for every fetch in the process
setGlobalDispatcher(proxy);A ProxyAgent 수영장 연결, 그래서 연속 요청은 한 연결과 따라서 한 출구를 재사용 할 수 있습니다. 요청에 따라 신선한 출구를 만들면 에이전트를 만듭니다. pipelining: 0 으로 connections: 1, 또는 단순히 다른 사용 -sid- 논리 세션 당.
엑소서스#
axios의 자체 proxy 옵션은 HTTPS를 안정적으로 터널하지 않습니다. 표준 접근법은 에이전트입니다. https-proxy-agent 으로 proxy: false.
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-de-city-berlin-sid-cart42-ttl-15m:PASS@res.proxy-cyber.com:9000");
const client = axios.create({ httpsAgent: agent, httpAgent: agent, proxy: false, timeout: 30000 });
const { data } = await client.get("https://api.ipify.org");
console.log(data);got#
import got from "got";
import { HttpsProxyAgent } from "https-proxy-agent";
const agent = new HttpsProxyAgent("http://USER-cc-us:PASS@res.proxy-cyber.com:9000");
const body = await got("https://api.ipify.org", { agent: { https: agent, http: agent }, timeout: { request: 30000 } }).text();
console.log(body);양말5#
import fetch from "node-fetch";
import { SocksProxyAgent } from "socks-proxy-agent";
const agent = new SocksProxyAgent(process.env.PROXY_URL);
const response = await fetch("https://api.ipify.org", { agent });
console.log(await response.text());환경 변수#
undici는 읽지 않습니다 HTTPS_PROXY 기본적으로. 최근 undici version ship EnvHttpProxyAgent, 이는; 세계 파견자와 전체 프로세스로 설정, 제 3 자 라이브러리를 포함 하 여 fetch에 내장, 프록시를 통해 이동.
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
// HTTPS_PROXY=http://USER-cc-us:PASS@res.proxy-cyber.com:9000 NO_PROXY=localhost node app.js
setGlobalDispatcher(new EnvHttpProxyAgent());통화 및 교체#
- 1개
ProxyAgent끈끈한 세션당 (다른)-sid-), 그 세션의 모든 요청에 의해 공유. - 넓은 교체를 위해, 1개의 대리인을 지키고 그러나 연결 재사용을 해제하고, 또는 N 회의 ids와 둥근robin를 가진 N 대리인을 그(것)들을 실행하십시오.
- Watch
UND_ERR_SOCKET으로ECONNRESET: 주거 출구가 나갔다; 한 번 재시행, 다음 연결은 다른 것을 선택합니다.
자주 묻는 질문#
왜 axios는 HTTPS에 대한 내 프록시를 무시합니까?
내장 프록시 지원은 CONNECT 터널에 제한됩니다. httpsAgent와 설정 프록시로 https-proxy-agent를 사용하십시오: false so axios는 자체를 처리하지 않습니다.
몇 가지 요청만 프록시 할 수 있습니까?
예. 필요한 요청에 대한 파견자 또는 대리인을 통과하고 다른 사람을 비난; setGlobalDispatcher를 호출하지 않는 한 아무 것도 글로벌하지 않습니다.