Create An HTTPS Server And Run Application HTTPS In Node.js

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 
 
Node.js

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

Node.js

Node.js

Node.js

Node.js

Node.js

Node.js
 
STEP 2 - Set up OpenSSL for usage

In Windows, click Start > Run 

In the Open box, type CMD and click OK

Node.js

A command prompt window appears,

Type the following command at the prompt and press Enter:

cd \OpenSSL-Win32 

Node.js

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

Node.js
Restart computer (mandatory)

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

    Node.js
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

Node.js

Which will create the following files in bin directory

Node.js

STEP 5 - Code

File Name: MainApp.js
  1. varhttp = require('http');  
  2. varexpress = require('express');  
  3. varapp = express();  
  4. consthttpsPort = 1234;  
  5. varhttps = require('https');  
  6. varfs = require('fs');  
  7. varoptions = {  
  8.     key: fs.readFileSync('./key.pem''utf8'),  
  9.     cert: fs.readFileSync('./server.crt''utf8')  
  10. };  
  11. //console.log("KEY: ", options.key)  
  12. //console.log("CERT: ", options.cert)  
  13. varsecureServer = https.createServer(options, app).listen(httpsPort, () => {  
  14.     console.log(">> CentraliZr listening at port " + httpsPort);  
  15. });  
  16. app.get('/sach'function(req, res) {  
  17.     res.sendFile(__dirname + '/public/index.htm');  
  18. });  
File Name - index.htm
  1. <html>  
  2.   
  3. <head>  
  4.     <title>My First HTTPS Secure Page</title>  
  5. </head>  
  6.   
  7. <body> Welcome to NodeJs Tutorials.... By Sachin Ghadi. </body>  
  8.   
  9. </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,

Node.js

STEP 6 - Code Execution

Install required packages like --> npm install express

Node.js

Execute code using the following command,

-->node MainApp.js 

Node.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

Node.js

Click on ADVANCED,

 Node.js

Now Click on Proceed to localhost(unsafe).

Node.js
Success!

One more step to go, geeks...

Append/search in your URL. Now, your URL will be https://localhost:1234/sach

Node.js

We have done it ... enjoy!

If you have any doubts regarding the same, just mention it in the comments .


Similar Articles