Setup a Static IP for Node.js using https-proxy-agent

Learn how to route Node.js HTTP traffic through a QuotaGuard Static IP using the https-proxy-agent library.

Prerequisites

npm install https-proxy-agent

Instructions

Run example
QUOTAGUARDSTATIC_URL=... node https.js

Be sure to set QUOTAGUARDSTATIC_URL to your HTTP proxy URL from the QuotaGuard Dashboard.

Test in Docker
docker build -t qg-static-node-https-example .
docker run -e QUOTAGUARDSTATIC_URL=... qg-static-node-https-example

Code Samples

HTTPS.js
var url = require('url');
var https = require('https');
var HttpsProxyAgent = require('https-proxy-agent');

// HTTP/HTTPS proxy to connect to
var proxy = process.env.QUOTAGUARDSTATIC_URL;
console.log('using proxy server %j', proxy);

// HTTPS endpoint for the proxy to connect to
var endpoint = 'https://ip.quotaguard.com';
console.log('attempting to GET %j', endpoint);
var options = url.parse(endpoint);

// create an instance of the `HttpsProxyAgent` class with the proxy server information
var agent = new HttpsProxyAgent(proxy);
options.agent = agent;

https.get(options, function (res) {
  console.log('"response" event!', res.headers);
  res.pipe(process.stdout);
});
Docker File
FROM node:latest

COPY https.js /app/https.js

WORKDIR /app/

RUN npm install https-proxy-agent

ENTRYPOINT [ "node", "https.js" ]