nodejs使用代理ip
安装模块
js
npm install https-proxy-agent http-proxy-agent使用
get请求示例
js
// get请求示例
const { HttpProxyAgent } = require('http-proxy-agent');
const agent = new HttpProxyAgent(`http://${ip}`); // 这里的ip就是自行购买的代理ip
const response = await axios.get(`XXXXXXXXX`, {
httpAgent: agent,
timeout: 15000
});post请求示例
js
// post请求示例
const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent('http:' + ip);
const re = await axios.get(baseUrl + '/api/auth/check', {
timeout: 80000, // 可选:8 秒超时
headers: { 'Content-Type': 'application/json' },
httpsAgent: agent,
})上面区别就是,同为http代理,但是get请求需要httpAgent ,post请求需要httpsAgent
