Node.js HTTPS Module

πŸ” Node.js – HTTPS Module

The HTTPS module in Node.js allows you to create secure web servers using SSL/TLS encryption.
HTTPS = HTTP + Encryption (SSL Certificate)

It ensures:

  • Secure data transfer

  • Protection from eavesdropping

  • Encryption between client and server

The module is built-in, so you don’t need to install anything.


βœ… 1. Import the HTTPS Module



 

You will also need:



 

(using SSL certificate files)


βœ… 2. Requirements for HTTPS Server

To run an HTTPS server, you need two files:

  • Private Key (key.pem)

  • Certificate (cert.pem)

For testing, you can generate them using OpenSSL:

openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -in csr.pem -signkey key.pem -out cert.pem

βœ… 3. Create a Simple HTTPS Server


 

βœ” Run:

node server.js

Open browser:

https://localhost:3001

βœ… 4. HTTPS with HTML Response



 


βœ… 5. HTTPS Routing Example



 


βœ… 6. Self-Signed Certificates Warning

If you generate your own certificate (self-signed):

  • Browser will show β€œNot Secure” warning

  • It is safe for development, not for production

For production use:

βœ” Let’s Encrypt (Free SSL)
βœ” Cloudflare SSL
βœ” Paid SSL from hosting providers


βœ… 7. HTTPS with Express.js (Optional)


 


βœ… 8. Difference Between HTTP and HTTPS

Feature HTTP HTTPS
Security ❌ No βœ… Yes (Encrypted)
Port 80 443
Certificate required No Yes
Safe for sensitive data No Yes
Browser padlock ❌ βœ…

🎯 Summary

Topic Description
Built-in module No installation required
Requires SSL certificate key.pem + cert.pem
Secure communication Encrypted data
Works like HTTP But secure
Used for production Secure APIs, payment systems, login systems

You may also like...