In this article, we will learn to create HTTPS self-signed certificate and create a Node.js application using HTTPS Server.
STEP 1 - Download openssl for Windows
Download your compactible Openssl setup from https://slproweb.com/products/Win32OpenSSL.html
STEP 1 - Download openssl for Windows
Download your compactible Openssl setup from https://slproweb.com/products/Win32OpenSSL.html

Double click the OpenSSL file using default settings to complete the installation.






STEP 2 - Set up OpenSSL for usage
In Windows, click Start > Run
In the Open box, type CMD and click OK
A command prompt window appears,
Type the following command at the prompt and press Enter:
cd \OpenSSL-Win32
The line changes to C:\OpenSSL-Win32
Type the following command at the prompt and press Enter,
set OPENSSL_CONF=c:\OpenSSL-Win32\bin\openssl.cfg

Restart computer (mandatory)
STEP 3 - Generate a Certificate Signing Request (CSR) using OpenSSL on Windows
In Windows, click Start > Run
In the Open box, type CMD and click OK

A command prompt window appears,
Type the following command at the prompt and press Enter:
cd \OpenSSL-Win32

The line changes to C:\OpenSSL-Win32
Type the following command at the prompt and press Enter,
set OPENSSL_CONF=c:\OpenSSL-Win32\bin\openssl.cfg

Restart computer (mandatory)
STEP 3 - Generate a Certificate Signing Request (CSR) using OpenSSL on Windows
- In Windows, click Start > Run
- In the Open box, type CMD and click OK

A command prompt window appears.
Type the following command at the prompt and press Enter:
cd \:OpenSSL-Win32\bin
The line changes to C:\OpenSSL-Win32\bin
Type the following command at the prompt and press Enter:
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
Then Type the following command at the prompt and press Enter:
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt
Which will create the following files in bin directory
STEP 5 - Code
File Name: MainApp.js
Type the following command at the prompt and press Enter:
cd \:OpenSSL-Win32\bin
The line changes to C:\OpenSSL-Win32\bin
Type the following command at the prompt and press Enter:
openssl req -newkey rsa:2048 -new -nodes -keyout key.pem -out csr.pem
Then Type the following command at the prompt and press Enter:
openssl x509 -req -days 365 -in csr.pem -signkey key.pem -out server.crt

Which will create the following files in bin directory

STEP 5 - Code
File Name: MainApp.js
- varhttp = require('http');
- varexpress = require('express');
- varapp = express();
- consthttpsPort = 1234;
- varhttps = require('https');
- varfs = require('fs');
- varoptions = {
- key: fs.readFileSync('./key.pem', 'utf8'),
- cert: fs.readFileSync('./server.crt', 'utf8')
- };
- //console.log("KEY: ", options.key)
- //console.log("CERT: ", options.cert)
- varsecureServer = https.createServer(options, app).listen(httpsPort, () => {
- console.log(">> CentraliZr listening at port " + httpsPort);
- });
- app.get('/sach', function(req, res) {
- res.sendFile(__dirname + '/public/index.htm');
- });
File Name - index.htm
- <html>
- <head>
- <title>My First HTTPS Secure Page</title>
- </head>
- <body> Welcome to NodeJs Tutorials.... By Sachin Ghadi. </body>
- </html>
Copy your csr.pem,key.pem,server.cert from C:\OpenSSL-Win32\bin to your applications root directory.
Create new folder in your directory and rename it as public and copy index.htm into it. The Folder Sturcture will look like this,
STEP 6 - Code Execution
Install required packages like --> npm install express
Execute code using the following command,
-->node MainApp.js
Our application is now runing on 1234 port using localhost.
Now, open the latest browser (I am using Chrome) and enter the following URL.
https://localhost:1234
Click on ADVANCED,
Now Click on Proceed to localhost(unsafe).
Success!
One more step to go, geeks...
Append/search in your URL. Now, your URL will be https://localhost:1234/sach
We have done it ... enjoy!
If you have any doubts regarding the same, just mention it in the comments .
Create new folder in your directory and rename it as public and copy index.htm into it. The Folder Sturcture will look like this,

STEP 6 - Code Execution
Install required packages like --> npm install express

Execute code using the following command,
-->node MainApp.js

Our application is now runing on 1234 port using localhost.
Now, open the latest browser (I am using Chrome) and enter the following URL.
https://localhost:1234

Click on ADVANCED,

Now Click on Proceed to localhost(unsafe).

One more step to go, geeks...
Append/search in your URL. Now, your URL will be https://localhost:1234/sach

We have done it ... enjoy!
If you have any doubts regarding the same, just mention it in the comments .
balaji manoharanPosted Mar 27, 2019, 5:25 AM
Not working in chrome
Armando GomezPosted Mar 1, 2019, 2:09 PM
Hello, thank you very much for your contribution, it is very well explained, but I have a question.Why is it still showing the legend "it's not safe"?