Categories
JavaScript Answers

How to add custom certificate authority (CA) to Node.js?

Spread the love

To add custom certificate authority (CA) to Node.js, we call the options.ca.push method.

For instance, we write

const trustedCa = [
  "/etc/pki/tls/certs/ca-bundle.crt",
  "/path/to/custom/cert.crt",
];

https.globalAgent.options.ca = [];
for (const ca of trustedCa) {
  https.globalAgent.options.ca.push(fs.readFileSync(ca));
}

to loop through each trusrtedCa path with a for-of loop.

In it, we call https.globalAgent.options.ca.push with the file read from readFileSync to add the certificate file into the array with push.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *