HTTPS with SSL

Node js 8 min min read Updated: Mar 30, 2026 Advanced
HTTPS with SSL
Advanced Topic 5 of 8

HTTPS with SSL in Node.js

Security is a critical part of modern web development. When data travels between a user’s browser and your server, it should be encrypted so that attackers cannot easily read or modify it. This is why production applications use HTTPS instead of plain HTTP.

HTTPS works by using SSL/TLS certificates to encrypt communication between the client and the server. In Node.js applications, HTTPS can be enabled either directly inside Node.js or more commonly through a reverse proxy like Nginx.

Key Concept: HTTPS uses SSL/TLS certificates to encrypt communication between the client and server, making web applications more secure.

What is HTTPS?

HTTPS stands for HyperText Transfer Protocol Secure. It is the secure version of HTTP and is used to protect data exchanged over the internet.

With HTTPS:

  • Data is encrypted
  • The server identity is verified
  • Data tampering becomes harder

This is especially important for websites and APIs that handle login credentials, payments, personal data, or sensitive business information.

What is SSL/TLS?

SSL (Secure Sockets Layer) is the older term commonly used for secure certificates, while TLS (Transport Layer Security) is the modern protocol actually used today. However, many people still say “SSL certificate” even when they mean TLS.

In practical terms, SSL/TLS certificates enable HTTPS for your application.

Why HTTPS is Important

  • Encrypts traffic: Protects sensitive user data
  • Builds trust: Browsers show secure connection indicators
  • Improves SEO: Secure sites are favored by search engines
  • Required for many features: Some browser APIs only work over HTTPS
  • Protects authentication: Keeps tokens, cookies, and credentials safer

HTTP vs HTTPS

Feature HTTP HTTPS
Encryption No Yes
Data Security Low High
Trust Level Lower Higher
Browser Warnings Possible Usually secure indication

How HTTPS Works

When a user visits a secure website, the browser and server perform an SSL/TLS handshake. During this process:

  1. The server provides its certificate
  2. The browser verifies the certificate
  3. A secure encrypted connection is established
  4. All further communication is protected

Using HTTPS Directly in Node.js

Node.js provides the built-in https module, which allows you to enable HTTPS directly in the application.

javascript const https = require("https"); const fs = require("fs"); const express = require("express"); const app = express(); app.get("/", (req, res) => { res.send("Secure Node.js app running with HTTPS"); }); const options = { key: fs.readFileSync("key.pem"), cert: fs.readFileSync("cert.pem") }; https.createServer(options, app).listen(443, () => { console.log("HTTPS server running on port 443"); });

In this example:

  • key.pem contains the private key
  • cert.pem contains the SSL certificate
  • The app listens securely on port 443

Understanding the Certificate Files

To enable HTTPS, you typically need:

  • Private Key: Secret key used by the server
  • Certificate: Public certificate issued for your domain
  • CA Bundle / Chain: Additional certificate chain sometimes required by providers

These files are usually provided by a certificate authority such as Let’s Encrypt or commercial SSL providers.

Self-Signed Certificates for Local Development

During local development, developers often use self-signed certificates for testing HTTPS.

bash openssl req -nodes -new -x509 -keyout key.pem -out cert.pem

This generates a private key and certificate for local use. However, browsers usually do not fully trust self-signed certificates in production.

Using Nginx for HTTPS in Production

In production, HTTPS is often handled by Nginx instead of directly inside Node.js. This is a more common setup because Nginx manages certificates, renewals, and reverse proxy behavior more efficiently.

nginx server { listen 443 ssl; server_name example.com; ssl_certificate /etc/ssl/certs/fullchain.pem; ssl_certificate_key /etc/ssl/private/privkey.pem; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

Here:

  • Nginx handles HTTPS on port 443
  • The Node.js app can continue running internally on port 3000
  • Nginx forwards requests securely to the app

Redirecting HTTP to HTTPS

It is common to redirect all HTTP traffic to HTTPS so users always access the secure version of the application.

nginx server { listen 80; server_name example.com; return 301 https://$host$request_uri; }

This ensures that any request coming to HTTP is automatically sent to HTTPS.

Using Let's Encrypt Certificates

Let’s Encrypt is a widely used free certificate authority that provides SSL/TLS certificates for websites and applications.

Many production deployments use Let’s Encrypt because it is free, trusted by browsers, and supports automatic renewal.

On Linux servers, certificates are often managed using tools like Certbot.

bash sudo certbot --nginx -d example.com -d www.example.com

This typically sets up the certificate and configures Nginx automatically.

Environment Variables for Certificate Paths

If you are enabling HTTPS directly in Node.js, certificate paths should not be hardcoded unnecessarily. You can store them in environment variables.

javascript require("dotenv").config(); const options = { key: fs.readFileSync(process.env.SSL_KEY_PATH), cert: fs.readFileSync(process.env.SSL_CERT_PATH) };

Security Best Practices for HTTPS

  • Use trusted SSL/TLS certificates in production
  • Redirect all HTTP traffic to HTTPS
  • Do not expose private key files publicly
  • Use Nginx or a load balancer for SSL termination in production
  • Renew certificates before they expire
  • Combine HTTPS with secure cookies and proper authentication practices

Common Mistakes

  • Using self-signed certificates in production
  • Forgetting to redirect HTTP to HTTPS
  • Hardcoding certificate files in insecure locations
  • Letting certificates expire
  • Assuming HTTPS alone solves all security issues

HTTPS and Secure Cookies

When using sessions, JWT cookies, or refresh tokens, HTTPS is especially important because secure cookies work best over encrypted connections.

This helps protect authentication-related data during transit.

Real-World Use Cases

  • Secure login systems
  • Payment gateways and checkout pages
  • Production REST APIs
  • Admin panels and dashboards
  • Real-time applications with secure connections

Direct HTTPS in Node.js vs Nginx SSL Termination

Feature Direct Node.js HTTPS Nginx HTTPS
Setup simplicity Simple for small apps Better for production
Certificate management Manual inside app/server Easier and more standard
Static file serving Basic Better
Load balancing Limited Strong support

Conclusion

HTTPS with SSL/TLS is essential for securing modern Node.js applications. It protects data in transit, improves trust, and is required for many production-grade features and security practices.

Whether you enable HTTPS directly inside Node.js or more commonly through Nginx, understanding SSL certificate setup is a key skill for backend deployment and production readiness.

Quick Summary: HTTPS uses SSL/TLS certificates to secure communication between users and your Node.js application, making production systems safer and more trustworthy.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators