axios
安装两个包
socks-proxy-agent,axios
const { SocksProxyAgent } = require('socks-proxy-agent');
const axios = require('axios');
const socks5Axios = axios.create();
const socks5 = () => {
const socks5Agent = new SocksProxyAgent("socks5://112.194.88.24:13314");
socks5Axios.defaults.httpsAgent = socks5Agent;
socks5Axios.defaults.proxy = false;
socks5Axios.get("https://ipinfo.io").then((res) => {
console.log(res.data);
}).catch((err) => {
console.log(err.message);
});
};
socks5()

fetch
安装一个包
socks-proxy-agent
const { SocksProxyAgent } = require('socks-proxy-agent');
const socks5Agent = new SocksProxyAgent("socks5://220.161.241.29:11315");
async function fetchWithSocks5() {
try {
const response = await fetch('https://ipinfo.io/json', {
method: "GET",
agent: socks5Agent,
timeout: 30000
});
if (!response.ok) {
throw new Error(`状态码错误: ${response.status}`);
}
const res_msg = await response.text()
console.log(res_msg);
return response
} catch (err) {
console.error('fetch 代理请求失败:', err.message);
}
}
fetchWithSocks5()
评论已关闭