-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode-fetch-proxy.js
More file actions
34 lines (29 loc) · 1.05 KB
/
node-fetch-proxy.js
File metadata and controls
34 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env node
/**
* node-fetch with proxy example.
*
* Configuration via environment variables:
* PROXY_URL - Proxy URL (required), e.g., http://user:pass@proxy:8080
* TEST_URL - URL to request (default: https://api.ipify.org?format=json)
*
* Note: node-fetch does not support sending custom headers to the proxy during
* HTTPS CONNECT tunneling, nor does it expose proxy response headers.
*/
import fetch from 'node-fetch';
import { HttpsProxyAgent } from 'https-proxy-agent';
const proxyUrl = process.env.PROXY_URL || process.env.HTTPS_PROXY;
if (!proxyUrl) {
console.error('Error: Set PROXY_URL environment variable');
process.exit(1);
}
const testUrl = process.env.TEST_URL || 'https://api.ipify.org?format=json';
const agent = new HttpsProxyAgent(proxyUrl);
try {
const response = await fetch(testUrl, { agent });
console.log(`Status: ${response.status}`);
const body = await response.text();
console.log(`Body: ${body}`);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}