Trả lời nhanh
Làm thế nào để sử dụng proxy thành phố Mỹ trong Node.js?
Tạo một undici ProxyAgent cho http://gw.metroproxies.com:7777 với tên người dùng thành phố như USERNAME-country-us-state-fl-city-orlando, và truyền nó làm dispatcher của fetch; Playwright nhận cùng các giá trị trong tùy chọn proxy của nó. MetroProxies bao gồm nhắm mục tiêu theo thành phố, ZIP và ASN trên mọi gói từ 0,50 US$/GB (tháng 10 năm 2026). Không cần KYC; tiền mã hóa.
import { ProxyAgent, fetch } from 'undici';
const user = 'USERNAME-country-us-state-fl-city-orlando';
const agent = new ProxyAgent({
uri: 'http://gw.metroproxies.com:7777',
token: 'Basic ' + Buffer.from(`${user}:PASSWORD`).toString('base64'),
});
const res = await fetch('https://ipinfo.io/json', { dispatcher: agent, signal: AbortSignal.timeout(30000) });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
console.log(await res.json());
await agent.close();
import { ProxyAgent, fetch } from 'undici';
const cities = [['fl', 'orlando'], ['fl', 'tampa'], ['ga', 'atlanta']];
for (const [state, city] of cities) {
const user = `USERNAME-country-us-state-${state}-city-${city}`;
const agent = new ProxyAgent({
uri: 'http://gw.metroproxies.com:7777',
token: 'Basic ' + Buffer.from(`${user}:PASSWORD`).toString('base64'),
});
try {
const res = await fetch('https://ipinfo.io/json', { dispatcher: agent, signal: AbortSignal.timeout(30000) });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const info = await res.json();
console.log(city, '->', info.ip, info.city, info.region);
} catch (err) {
console.log(city, 'failed:', err.message);
} finally {
await agent.close();
}
}
import { chromium } from 'playwright';
const browser = await chromium.launch({
proxy: {
server: 'http://gw.metroproxies.com:7777',
username: 'USERNAME-country-us-state-fl-city-miami-session-mia01-ttl-15',
password: 'PASSWORD',
},
});
const context = await browser.newContext({ timezoneId: 'America/New_York', locale: 'en-US' });
const page = await context.newPage();
await page.goto('https://ipinfo.io/json', { timeout: 60000 });
console.log(await page.textContent('body'));
await browser.close();
Tôi nên chú ý gì trong Node.js?
- Keep-alive: một
ProxyAgenttái sử dụng các kết nối, vì vậy trong chế độ xoay vòng, nó giữ cùng IP thoát khi một kết nối còn mở. Tạo một agent cho mỗi thành phố, hoặc đóng nó, để có IP mới. - Múi giờ và ngôn ngữ: các tùy chọn
timezoneIdvàlocalecủa Playwright làm cho trình duyệt khớp với thành phố; mỗi trang thành phố cung cấp vùng IANA của nó (Miami:America/New_York). - SOCKS5: Chromium không gửi tên người dùng đến proxy SOCKS5; sử dụng cổng HTTP trong trình duyệt.
- Thời gian chờ:
AbortSignal.timeout()trong fetch,timeouttrong Playwright. - Tốc độ: giữ yêu cầu đến mỗi trang ở mức vừa phải và tuân thủ robots.txt và điều khoản của trang đó.
Tôi nên đi đâu tiếp theo?
Bắt đầu từ thiết lập năm phút, so sánh với các ví dụ Python và curl, và kiểm tra IP thoát với công cụ kiểm tra vị trí proxy. Tất cả tài liệu: tài liệu.
Người ta còn hỏi gì về Node.js?
Tôi cần phiên bản Node.js nào?
Node.js 18 trở lên để dùng AbortSignal.timeout và top-level await trong các tệp .mjs. Cài undici bằng npm install undici và Playwright bằng npm install playwright.
Puppeteer có dùng được không?
Có: khởi chạy với --proxy-server=http://gw.metroproxies.com:7777 và gọi page.authenticate({ username, password }) trước lần điều hướng đầu tiên.
Các ví dụ này có được chạy thử với gateway không?
Chúng được kiểm tra cú pháp bằng node --check, với USERNAME và PASSWORD là chỗ giữ chỗ. Hãy chạy chúng bằng thông tin đăng nhập của bạn.