How to fix the hostname or IP not matching certificate’s altnames with Node.js?

Sometimes, we want to fix the hostname or IP not matching certificate’s altnames with Node.js.

In this article, we’ll look at how to fix the hostname or IP not matching certificate’s altnames with Node.js.

How to fix the hostname or IP not matching certificate’s altnames with Node.js?

To fix the hostname or IP not matching certificate’s altnames with Node.js, we can use http-proxy with changeOrigin to true.

For instance, we write

const proxy = httpProxy.createProxyServer();

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

to create a proxy to proxy the traffic to example.com:3000.

Then we create the server with

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 read the key and certificate with fs.readFileSynbc and then we set target to the hostname we’re hosting our server from.

Conclusion

To fix the hostname or IP not matching certificate’s altnames with Node.js, we can use http-proxy with changeOrigin to true.