How to Install SSL Certificate on Node.js?
Steps to Install SSL Certificate on Node.js
Node.js is a free cross-platform server environment, useful to servers and operating systems like Linux, Windows, Mac, Unix, etc. It is used to execute JavaScript codes outside or without a browser.
To install an SSL certificate in Node.js, you will need the SSL certificate files. The certificate authority will provide the certificate files and the private key. You will have to upload these files to the server where the Node.js application is located.
A private key will be generated when your Certificate Signing Request (CSR) is generated. Make sure you store this private key securely on the server so that no one else can access it.
Step by Step Instructions to Install an SSL Certificate on Node.js
- Step 1: Download the SSL Certificate Files
- Step 2: Create https_server.js file and upload SSL certificate files
- Step 3: Activate Your SSL certificate on Node.js
Step 1: Download the SSL Certificate Files
If you have completed the entire order process and have purchased your SSL certificate, you will receive an email from the certificate authority (CA) with your SSL certificate files. This includes the Intermediate and Root certificates and the CA bundle files, as well. Remember, you will receive this email only after the CA verifies your documents after you complete the purchase.
You will need the following to proceed to the next step.
- Primary certificate for your domain
- Root certificate
- The CA bundle file with the intermediate and root certificates
- Your private key
The installation process involves the following steps. You will have to create an https_server.js file and upload files to the server directory. Followed by that, you can activate your SSL certificate on Node.js.
Step 2: Create https_server.js File and Upload SSL Files
Here, you can rename “Server.js” as you wish and the following values will help create your https server, the https_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)
In the above, replace the text in bold with the following.
- path/to/private.key – This is your private key file’s path.
- path/to/your_domain_name.crt – Enter your SSL certificate file’s path.
- path/to/CA_root.crt – Provide the CA root certificate file’s full path.
- path/to/ca_bundle_certificate – This is the full path of your uploaded CA bundle file.
Step 3: Activate SSL Certificate on Node.js
Running the following command will help launch the Node.js application.
# node https_server.js
Once you run this command, your SSL Certificate will be active on Node.js.
You can check whether your certificate has been installed using an online SSL checker tool.
Related Articles:
95%OFF
Comodo Positive SSL
78%OFF
RapidSSL Certificate
95%OFF