Node.js 常用 axios 发请求,设置代理只需在配置里加 proxy 字段。

基础代理设置

const axios = require('axios');

const agent = {
  host: 'IP',
  port: 端口,
  auth: { username: '用户名', password: '密码' },
};

axios.get('https://httpbin.org/ip', {
  proxy: agent,
  timeout: 10000,
}).then(res => console.log(res.data));

使用 http-proxy-agent / https-proxy-agent

const { HttpsProxyAgent } = require('https-proxy-agent');

const agent = new HttpsProxyAgent('http://用户名:***@IP:端口');

axios.get('https://httpbin.org/ip', {
  httpsAgent: agent,
  timeout: 10000,
}).then(res => console.log(res.data));
Node 的 https-proxy-agent 包能同时处理 http 和 https 请求,推荐使用。