Categories
JavaScript Answers

How to fix hostname/IP does not match certificate’s altnames with Node.js and JavaScript?

Spread the love

To fix hostname/IP does not match certificate’s altnames with Node.js and JavaScript, we create a proxy.

For instance, we write

const httpProxy = require("http-proxy");
const proxy = httpProxy.createProxyServer();

proxy.web(req, res, {
  changeOrigin: true,
  target: "https://example.com:3000",
});

to create a proxy with the http-proxy package.

We call createProxyServer to create the proxy.

And we set the target to the destination URL to proxy to.

If we’re using HTTPS, we write

const httpProxy = require("http-proxy");

httpProxy
  .createServer({
    ssl: {
      key: fs.readFileSync("valid-ssl-key.pem", "utf8"),
      cert: fs.readFileSync("valid-ssl-cert.pem", "utf8"),
    },
    target: "https://example.com:3000",
    secure: true,
  })
  .listen(443);

to call createServer to create the proxy server.

We set the SSL key and certificate files.

And we set the target to the destination URL to proxy to.

We set secure to true to let us proxy HTTPS traffic.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.