Nodejs的https或axios使用代理请求

折腾了大半天,搞定了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)
});

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

你可能感兴趣的:(Nodejs/Vue,node.js,axios,proxy)