In this article, you will find out how to install an SSL Certificate on Node.js. The SSL configuration requires all the certificate files including root, intermediate, and primary certificates. You should have these files in an archived folder received from your Certificate Authority.
After the installation, continue reading to discover the best place online where you can get an SSL certificate for your Node.js platform.
Table of Contents
Generate a CSR code in Node.js
If you haven’t applied for an SSL Certificate yet, you will need to generate a CSR code for Node.js. This step is necessary since the CSR (Certificate Signing Request) contains your contact details.
You have two options:
- Generate the CSR automatically using our CSR Generator.
- Follow our step-by-step tutorial on how to create the CSR in Node.js
Install an SSL Certificate on Node.js
Step 1: Prepare all your certificate files
Here’s what you’re going to need:
- The primary certificate for your domain (.crt extension)
- The root certificate (.crt)
- The Ca Bundle file containing the root and intermediate certificates. (.ca-bundle extension)
- Your private key generated along with CSR (.key extension)
Step 2: Create an HTTPS server in Node.js environment
In the command line, use the following values to create your HTTPS server. For this demonstration, we’ve named it https_server.js, but you can give any name to the server.js file
#vim https_server.js
var https = require('https');
var fs = require('fs');
var https_options = {
key: fs.readFileSync("/path/to/private.key"),
cert: fs.readFileSync("/path/to/your_domain_name.crt"),
ca: [
fs.readFileSync('path/to/CA_root.crt'),
fs.readFileSync('path/to/ca_bundle_certificate.crt')
] };
https.createServer(options, function (req, res) {
res.writeHead(200);
res.end("Welcome to Node.js HTTPS Servern");
}).listen(8443)
You need to replace the parts in bold with your corresponding information.
- path/to/private.key – indicate the full path to your private key file
- path/to/your_domain_name.crt – specify the correct path to your SSL certificate file
- path/to/CA_root.crt – type the full path of the CA root certificate file
- path/to/ca_bundle_certificate – enter the full path of to your CA bundle file
Step 3: Activate your SSL Certificate on Node.js
Run the following command to launch the Node.js app:
# node https_server.js
Step 4: Test your SSL installation
Test your SSL installation for potential errors or vulnerabilities using one of these SSL checker tools.
Save 10% on SSL Certificates when ordering today!
Fast issuance, strong encryption, 99.99% browser trust, dedicated support, and 25-day money-back guarantee. Coupon code: SAVE10