Shifterと一緒に使用する Node.js
ShifterのレジデンシャルおよびISP Proxyを任意のNode.jsプロジェクトに数分で組み込めます。axios、ネイティブのfetch(Node 18+)、got、undici、Puppeteer、Playwrightとシームレスに連携します。
クイックスタート
インストール
npm install axios https-proxy-agent 基本的な使い方
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
const proxyUrl =
"customer-USERNAME-country-us-sid-123ABC:PASSWORD@p.shifter.io:443";
const agent = new HttpsProxyAgent(proxyUrl);
const { data } = await axios.get("https://ipinfo.io/json", {
httpsAgent: agent,
proxy: false,
});
console.log(data);
// { ip: "154.16.xxx.xxx", city: "New York", country: "US", ... } 機能
例
axios + ジオターゲティングスティッキーセッション
ユーザー名に `sid` を追加することで、ユーザーセッション全体を通じてレジデンシャル IP を 1 つ固定できます。地理的なターゲティングには `country-uk`、`city-london`、または `asn-7922` を追加してください。
import axios from "axios";
import { HttpsProxyAgent } from "https-proxy-agent";
import { randomBytes } from "node:crypto";
const sid = randomBytes(4).toString("hex");
const proxyUrl =
`customer-USERNAME-country-uk-city-london-sid-${sid}-ttl-300:` +
`PASSWORD@p.shifter.io:443`;
const client = axios.create({
httpsAgent: new HttpsProxyAgent(proxyUrl),
proxy: false,
timeout: 30_000,
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
},
});
const login = await client.post("https://example.com/login", {
email: "user@example.com",
password: "secret",
});
const dashboard = await client.get("https://example.com/dashboard");
const orders = await client.get("https://example.com/orders");
console.log(login.status, dashboard.status, orders.status); ネイティブ fetch(Node 18+)と undici
Node 18 以降をお使いの場合、axios は不要です。undici の ProxyAgent を使ったグローバル fetch を使用してください。フットプリントを最小限に抑えられます。
import { ProxyAgent, setGlobalDispatcher } from "undici";
const proxyAgent = new ProxyAgent({
uri: "http://p.shifter.io:443",
token: `Basic ${Buffer.from(
"customer-USERNAME-country-us-sid-456DEF:PASSWORD",
).toString("base64")}`,
});
setGlobalDispatcher(proxyAgent);
const r = await fetch("https://api.example.com/products?page=1");
const json = await r.json();
console.log(json); 認証済みプロキシを使用した Puppeteer
Chromiumをpage.authenticate()でShifterを通じて認証しながらルーティングします。`sid`とブラウザレベルのクッキーを組み合わせることで、クロール全体を通じて安定したセッションを維持できます。
import puppeteer from "puppeteer";
const PROXY_HOST = "p.shifter.io";
const PROXY_PORT = 443;
const PROXY_USER = "customer-USERNAME-country-de-sid-789GHI";
const PROXY_PASS = "PASSWORD";
const browser = await puppeteer.launch({
args: [`--proxy-server=http://${PROXY_HOST}:${PROXY_PORT}`],
headless: "new",
});
const page = await browser.newPage();
await page.authenticate({ username: PROXY_USER, password: PROXY_PASS });
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
);
await page.goto("https://example.com", { waitUntil: "networkidle0" });
const title = await page.title();
const html = await page.content();
console.log(title, html.length, "bytes");
await browser.close(); Playwright(マルチブラウザ)
Playwright の起動オプションはプロキシをネイティブに受け入れ、認証情報をインラインで指定できます。同じ設定が Chromium、Firefox、および WebKit で機能します。
import { chromium } from "playwright";
const browser = await chromium.launch({
proxy: {
server: "http://p.shifter.io:443",
username: "customer-USERNAME-country-fr-city-paris-sid-ABC123",
password: "PASSWORD",
},
});
const context = await browser.newContext({
userAgent:
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
});
const page = await context.newPage();
await page.goto("https://example.com");
const headlines = await page.locator(".headline").allTextContents();
console.log(headlines);
await browser.close(); よくある質問
Shifter と Node.js の併用に関するよくある質問。
httpsAgentオプションにhttps-proxy-agent(またはプレーンHTTPの場合はhttp-proxy-agent)を渡し、axiosがエージェントを直接使用するようにproxy: falseを設定してください。プロキシURLの形式は`http://USER:PASS@p.shifter.io:443`です。同じaxiosインスタンスをリクエスト間で再利用できます。
はい。Node 18以降のグローバルfetchは内部でundiciを使用しています。プロキシURIとベーシック認証トークンを指定してundiciのProxyAgentを作成し、setGlobalDispatcher(proxyAgent)を呼び出してください。以降のすべてのfetch呼び出しがShifterを経由してルーティングされます。
ユーザー名にセッションIDを追加してください。例:`customer-USERNAME-country-us-sid-123ABC`。同じsidを共有するすべてのリクエストは同じレジデンシャルIPを再利用します。`ttl-300`を追加すると、そのIPを300秒まで保持できます。
はい。Puppeteerの場合は、起動引数に --proxy-server を渡し、プロキシ認証情報を使って page.authenticate() を呼び出してください。Playwrightの場合は、起動オプションのproxyフィールドにserver、username、passwordを設定します。Chromium、Firefox、WebKitで同様に動作します。
プロキシのユーザー名に国、地域、都市、ASNのセレクターを追加します。例えば`customer-USERNAME-country-uk-city-london-sid-456DEF`はロンドンのIPを返します。同じ構文がすべてのNode HTTPライブラリで動作します。SDKは不要です。
SDKは不要です。Shifterのゲートウェイはシングルエンドポイントで標準のHTTP / SOCKS5に対応しています。すでに使用しているHTTPクライアントでプロキシを設定するだけで完了です。
でShifterを使い始める Node.js
Shifterの2億500万件以上のレジデンシャル・ISPプロキシを5分以内にNode.jsスタックに追加できます。リクエストごとのローテーション・スティッキーセッション・Puppeteer / Playwrightのフルサポートに対応しています。