折腾了大半天,搞定了nodejs中使用代理!!
https模块的使用参考这个:
几行代码实现在nodejs中代理请求,可以使用 https 或者 axios,需要使用第三方库
npm install socks-proxy-agent
在业务中使用:
const axios = require('axios')
const SocksProxyAgent = require('socks-proxy-agent')
const httpsAgent = new SocksProxyAgent.SocksProxyAgent('socks5://127.0.0.1:8088')
axios.get("www.google.com", { httpsAgent }).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error)
})
//post json
const headerJSON = {
"Content-Type": "application/json"
}
axios.post("www.google.com", JSON.stringify(data),
{headers: headerJSON,httpsAgent}).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error)
});
这样即可实现走代理请求对应的链接。