This page looks best with JavaScript enabled

Using Node.js to Proxy HTTPS Requests to Dependent R&D Services

 ·  ☕ 3 min read

1. Background

Under a microservices architecture, the dependency relationships between services are complex. During development, multiple services often need joint debugging. There are two ways to do this:

  • Deploy services to the online development environment, the Kubernetes cluster

Use telepresence to connect local and online cluster communication, so you get a relatively stable joint debugging environment.

The drawback is that you need to generate credentials with sufficient privileges, and R&D staff need to be familiar with using Kubernetes. One set per person is also relatively expensive.

  • Call each other directly between development machines on the office network

The intranet is interconnected, so direct calls are also feasible. But some service dependencies hard-code HTTPS and the domain name, so you need to set up an HTTPS proxy service locally.

This article mainly introduces how to use Node.js to build an HTTPS proxy service for forwarding requests to dependent services.

2. Proxy Logic

As shown above, this is a schematic of proxying HTTPS requests. The main steps are:

  1. Generate a CA certificate

  2. Issue a service domain certificate

  3. Configure the proxy service

  4. Make the device trust the CA certificate

  5. Configure Hosts or DNS access

For the specific operations of generating a CA certificate and issuing a service domain certificate, see Harbor Uses a Self-Signed Certificate to Support HTTPS Access.

3. Configuring the Proxy

  • Install dependencies
1
npm install express http-proxy-middleware
  • Configure proxy.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const https = require("https");
const express = require("express");
const { createProxyMiddleware } = require("http-proxy-middleware");
const fs = require("fs");

const options = {
  cert: fs.readFileSync("www.baidu.com.cert"),
  key: fs.readFileSync("www.baidu.com.key"),
};

const app = express();

app.use(
  "/",
  createProxyMiddleware({
    target: "http://2.2.2.2:8080",
    changeOrigin: true,
  }),
);

https.createServer(options, app).listen(443, () => {
  console.log("Proxy server listening on port 443");
});
  • Start the proxy service
1
node proxy.js

At this point, by configuring Hosts or DNS (1.1.1.1 www.baidu.com) and accessing https://www.baidu.com, you can reach the http://2.2.2.2:8080 service.

  • Specifying the target domain and its resolution

If you want to configure target as a domain name and specify the resolution yourself, you can add a DNS snippet, which takes effect only in the current service.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const dns = require("dns");

const customLookup = (hostname, options, callback) => {
  const customIP = "2.2.2.2";
  const family = options.family || 4;

  if (hostname === "target.domain.com") {
    const address = family === 6 ? "::1" : customIP;
    return callback(null, address, family);
  }
  dns.lookup(hostname, options, callback);
};
dns.lookup = customLookup;

WeChat Official Account
WRITTEN BY
WeChat Official Account