Learn how to securely route Node.js HTTP traffic through a QuotaGuard Shield proxy using the Axios library. Includes full code examples.
npm install axios https-proxy-agentRun example
QUOTAGUARDSHIELD_URL=... node https.js
Be sure to set QUOTAGUARDSHIELD_URL to your HTTP proxy URL from the QuotaGuard Dashboard.
TARGET_URL: The endpoint to connect to (defaults to https://ip.quotaguard.com)http:// or https://https://example.com:8443)
QUOTAGUARDSHIELD_URL=... node https.jsQUOTAGUARDSHIELD_URL=... TARGET_URL=https://httpbin.org/get node https.jsQUOTAGUARDSHIELD_URL=... TARGET_URL=https://example.com:8443 node https.jsdocker build -t qg-shield-node-https-axios-example .
docker run -e QUOTAGUARDSHIELD_URL=... qg-shield-node-https-axios-exampledocker run -e QUOTAGUARDSHIELD_URL=... -e TARGET_URL=https://httpbin.org/get qg-shield-node-https-axios-exampleconst axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const proxyUrl = process.env.QUOTAGUARDSHIELD_URL;
// HTTPS endpoint for the proxy to connect to
const endpoint = process.env.TARGET_URL || 'https://ip.quotaguard.com';
// Validate endpoint format
if (!endpoint.startsWith('https://') && !endpoint.startsWith('http://')) {
console.error('Error: TARGET_URL must start with "http://" or "https://"');
console.error('Current value:', endpoint);
process.exit(1);
}
const fetchIp = async () => {
try {
// Create an HTTPS agent that tunnels through the proxy
const httpsAgent = new HttpsProxyAgent(proxyUrl);
const axiosInstance = axios.create({
httpsAgent
});
const res = await axiosInstance.get(endpoint);
console.log('Status Code:', res.status);
console.log('Response headers:', res.headers);
console.log(res.data);
} catch (err) {
console.error('Error:', err.message);
if (err.response) {
console.error('Status Code:', err.response.status);
console.error('Response headers:', err.response.headers);
}
}
};
fetchIp();FROM node:latest
COPY https.js /app/https.js
WORKDIR /app/
RUN npm install axios https-proxy-agent
ENTRYPOINT [ "node", "https.js" ]