Nodejs的https或axios使用代理请求

本文介绍了如何在Node.js环境中,利用https模块和axios库,结合第三方库socks-proxy-agent实现代理请求。详细讲解了配置代理的步骤,并提供了在实际业务中应用的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

折腾了大半天,搞定了nodejs中使用代理!!

https模块的使用参考这个:

socks-proxy-agent - npmA SOCKS proxy `http.Agent` implementation for HTTP and HTTPS. Latest version: 7.0.0, last published: 2 months ago. Start using socks-proxy-agent in your project by running `npm i socks-proxy-agent`. There are 334 other projects in the npm registry using socks-proxy-agent.https://www.npmjs.com/package/socks-proxy-agent

几行代码实现在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)
});

这样即可实现走代理请求对应的链接。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值