Sometimes, we want to add custom certificate authority (CA) to Node.js.
In this article, we’ll look at how to add custom certificate authority (CA) to Node.js.
How to add custom certificate authority (CA) to Node.js?
To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push
with the certificate file.
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));
}
We loop through the path strings in trustedCa
with a for-of loop.
Then we call https.globalAgent.options.ca.push
to push the certificate file that we read into memory with fs.readFileSync
.
We set https.globalAgent.options.ca
to an array before calling push
so its value is always an array.
Conclusion
To add custom certificate authority (CA) to Node.js, we can call https.globalAgent.options.ca.push
with the certificate file.