ວິທີຕັ້ງຄ່າ proxy ໃນ Node.js (Axios & Native Fetch)
Updated:
ຕົວເລືອກ proxy: { host, port } ທີ່ມີມາພ້ອມຂອງ Axios ຈະໃຊ້ບໍ່ໄດ້ກັບປາຍທາງ https:// ເນື່ອງຈາກມັນບໍ່ໄດ້ສ້າງອຸໂມງ HTTP CONNECT. ເພື່ອສົ່ງຄຳຂໍ Node.js ແລະ TypeScript ຜ່ານ HTTP(S) proxy ທີ່ມີການຢືນຢັນຕົວຕົນຂອງ VinaProxy ໄດ້ຢ່າງລຽບງ່າຍ, ໃຫ້ສົ່ງ HttpsProxyAgent (https-proxy-agent) ໄປຍັງ Axios ຫຼື ສົ່ງ dispatcher ຂອງ undici.ProxyAgent ໄປຍັງ Node fetch() ແບບ native.
new HttpsProxyAgent('http://user:pass@ip:port')- 1 IP₫40.000
- 5+ IP₫35.000
- 10+ IP₫25.000
- 20+ IP₫25.000
- 50+ IP₫25.000
- 100+ IP₫25.000
- 200+ IP₫25.000
- 500+ IP₫25.000
- 1000+ IP₫22.000
- 2000+ IP₫20.000
- 1 IP₫40.000
- 5+ IP₫35.000
- 10+ IP₫25.000
- 20+ IP₫25.000
- 50+ IP₫25.000
- 100+ IP₫25.000
- 200+ IP₫25.000
- 500+ IP₫25.000
- 1000+ IP₫22.000
- 2000+ IP₫20.000
ການຕັ້ງຄ່າ proxy ລະຫວ່າງ Axios ກັບ Node.js fetch() ແບບ native
ທັງສອງ library ຮອງຮັບ URL ມາດຕະຖານ http://user:pass@ip:port ຈາກ dashboard ຂອງ VinaProxy:
| HTTP Client | Package | ໂຄດຕົວຢ່າງທີ່ແນະນຳ |
|---|---|---|
| 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' }) } }) |
ແນວທາງປະຕິບັດທີ່ດີທີ່ສຸດສຳລັບ connection pooling ແລະ timeout ໃນ Node.js
- ຕັ້ງຄ່າ
proxy: falseຄຽງຄູ່ກັບhttpsAgentໃນ Axios ສະເໝີ ເພື່ອໃຫ້ Axios ບໍ່ສົນໃຈຕົວປ່ຽນສະພາບແວດລ້ອມHTTP_PROXYຂອງລະບົບທີ່ຂັດແຍ້ງກັນ - ສ້າງ instance ຂອງ
HttpsProxyAgentຫຼືProxyAgentພຽງຄັ້ງດຽວຕໍ່ proxy IP ຢູ່ນອກ loop ເພື່ອນຳໃຊ້ persistent keep-alive socket ຊ້ຳ - ສົ່ງອອກລາຍການ proxy ຂອງ VinaProxy ເປັນ JSON array ໂດຍກົງດ້ວຍ Proxy Format Converter ຂອງພວກເຮົາ
ເທື່ອລະຂັ້ນຕອນ
- 1
ຕິດຕັ້ງ https-proxy-agent (ຫຼື undici)
ຣັນ
npm install axios https-proxy-agentໃນໂປຣເຈັກ Node.js ຂອງທ່ານ. - 2
ສ້າງ instance ຂອງ HttpsProxyAgent
ປະກາດ
const agent = new HttpsProxyAgent('http://user:pass@ip:port');ໂດຍໃຊ້ຂໍ້ມູນເຂົ້າສູ່ລະບົບຂອງ VinaProxy. - 3
ສົ່ງ httpsAgent ແລະ proxy: false ໄປຍັງ Axios
ຮຽກໃຊ້
const res = await axios.get('https://vinaproxy.com/api/my-ip', { httpsAgent: agent, proxy: false, timeout: 10000 });. - 4
ກວດສອບ IP ຂາອອກ
ສະແດງ
res.dataໃນ log ເພື່ອຢືນຢັນວ່າການຕອບກັບຖືກສົ່ງຜ່ານ IP proxy ສະເພາະຂອງທ່ານ.
ຄຳຖາມທີ່ຖາມເລື້ອຍໆ
ເປັນຫຍັງຄ່າເລີ່ມຕົ້ນ `proxy: { host, port, auth }` ຂອງ Axios ຈຶ່ງໃຊ້ບໍ່ໄດ້ກັບ HTTPS URL?
ການຕັ້ງຄ່າ proxy ທີ່ມີມາພ້ອມຂອງ Axios ຈະສົ່ງຄຳຂໍ HTTP ທຳມະດາ ແທນທີ່ຈະເປີດອຸໂມງ TLS ດ້ວຍ HTTP CONNECT. ການໃຊ້ httpsAgent: new HttpsProxyAgent('http://user:pass@ip:port') ພ້ອມກັບ proxy: false ຈະແກ້ໄຂບັນຫານີ້ໄດ້.
ຂ້ອຍຈະໃຊ້ proxy ກັບ `fetch()` ແບບ native ໃນ Node.js 18/20/22 ແນວໃດ?
Import ProxyAgent ຈາກ undici ແລະ ສົ່ງ { dispatcher: new ProxyAgent('http://user:pass@ip:port') } ໃນ object ຕົວເລືອກຂອງ fetch().
ຂ້ອຍສາມາດແປງລາຍການ proxy ຂອງຂ້ອຍເປັນ JSON ສຳລັບ Node.js ໄດ້ບໍ່?
ໄດ້—ວາງແຖວ ip:port:user:pass ຂອງທ່ານໃສ່ Proxy Format Converter ຟຣີຂອງພວກເຮົາ ແລ້ວເລືອກຜົນອອກເປັນ JSON.
ເພີ່ມປະສິດທິພາບ backend ແລະ scraper ໃນ Node.js ຂອງທ່ານດ້ວຍ static IP
Dedicated IPv4 ແລະ Static ISP proxy ບໍ່ຈຳກັດ bandwidth ພ້ອມໃຊ້ທັນທີໃນ 7 ປະເທດ.
ຊື້ Proxy ສຳລັບ Node.js