Last active
June 17, 2021 21:40
-
-
Save abd1rahmane/e3a95c228b950c80fb5eaba202134e55 to your computer and use it in GitHub Desktop.
Axios with Proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const { HttpProxyAgent, HttpsProxyAgent } = require("hpagent"); | |
let useProxy = true | |
if (useProxy) { | |
var proxy = "http://USERNAME:PASSWORD@PROXYIP:PROXYPORT"; | |
const httpsAgent = new HttpsProxyAgent({ | |
keepAlive: true, | |
keepAliveMsecs: 2000, | |
maxSockets: 256, | |
maxFreeSockets: 256, | |
proxy: proxy, | |
}); | |
const httpAgent = new HttpProxyAgent({ | |
keepAlive: true, | |
keepAliveMsecs: 2000, | |
maxSockets: 256, | |
maxFreeSockets: 256, | |
proxy: proxy, | |
}); | |
axios.defaults.httpAgent = httpAgent | |
axios.defaults.httpsAgent = httpsAgent | |
} | |
async function getIP() { | |
const config = { | |
method: "GET", | |
url: "https://api.ipify.org?format=json", | |
}; | |
try { | |
let res = await axios.request(config); | |
console.log(res.data); | |
} catch (error) { | |
console.log(error.message); | |
} | |
} | |
getIP(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment