node.js 18.3 使用代理的本机获取请求

ded*_*v22 7 proxy node.js fetch-api

我想使用本机 fetch node.js 18.3 通过代理发出请求,但我不知道如何操作。

我的代码:

import proxyAgent from 'proxy-agent';

(async () => {
    const res = await fetch('https://api.ipify.org?format=json', {
        agent: new proxyAgent('http://80.48.119.28:8080'),
    });
    const data = await res.json();
    console.log(data); //it shows my ip
})();
Run Code Online (Sandbox Code Playgroud)

Tah*_*abi 0

您需要将代理放在请求标头中

import proxyAgent from 'proxy-agent';

(async () => {
    const res = await fetch('https://api.ipify.org?format=json', {
      headers: {
        agent: new proxyAgent('http://80.48.119.28:8080'),
      }
    });
    const data = await res.json();
    console.log(data); //it shows my ip
})();
Run Code Online (Sandbox Code Playgroud)