Chapter 23: Node.js HTTPS Module

1. What is the https module and how is it different from http?

node:https is almost identical to node:http, but it adds TLS/SSL encryption.

Feature node:http node:https
Protocol HTTP (unencrypted) HTTPS (encrypted via TLS/SSL)
Default port 80 443
Security No encryption – data is readable Encrypted – protects data in transit
Certificate required No Yes (server certificate + private key)
Client certificate auth Not applicable Possible (mutual TLS)
Performance overhead Lower Slightly higher (TLS handshake)
Modern usage Development, internal APIs Almost all production APIs

Key takeaway 2026: You almost never run a public-facing server with plain http anymore — HTTPS is the default expectation (browsers show warnings, SEO penalty, security compliance).

2. Creating the most basic HTTPS server

You need at least two files:

  1. Private key (.key)
  2. Certificate (.crt / .pem)

For real learning & development, the easiest way is to use self-signed certificates.

Bash

This creates:

  • server.key → private key
  • server.crt → public certificate

Now the code:

JavaScript

Run:

Bash

Open browser → https://localhost:3443

You will see a security warning because the certificate is self-signed.

Click “Advanced” → “Proceed to localhost (unsafe)” to continue (only for learning).

3. Using a real certificate in development (recommended way 2026)

Instead of self-signed certificates that trigger warnings, most developers use mkcert — a zero-config tool that creates locally trusted certificates.

Install mkcert (one-time):

Bash

Then generate certificate for localhost:

Bash

→ creates localhost+2.pem and localhost+2-key.pem

Now update the code:

JavaScript

→ Browser will show green padlock → no warnings

4. Realistic example – JSON API over HTTPS

JavaScript

Try with curl (note the -k flag for self-signed, or remove if using mkcert):

Bash

5. Making outgoing HTTPS requests (client side)

Modern way (Node.js 18+):

JavaScript

Classic way (still useful for more control):

JavaScript

6. Important security & production considerations

Topic Recommendation 2025–2026 Why it matters
Self-signed certificates Only for local development Browsers & clients reject them
Real certificates Use Let’s Encrypt (free), ZeroSSL, or paid (Cloudflare, AWS ACM) Required for public HTTPS
HTTP → HTTPS redirect Always redirect 80 → 443 Security & SEO
HSTS header Add Strict-Transport-Security header Forces browser to always use HTTPS
Minimum TLS version TLS 1.3 (or at least 1.2) Old versions are insecure
Cipher suites Use secure defaults or Mozilla’s intermediate config Protects against known attacks
Client certificate auth Use when needed (mutual TLS) Strong authentication

Very common production setup (not raw https):

JavaScript

Summary – Quick comparison & decision guide

Situation Use this in 2026 Why?
Local development / learning https + mkcert Trusted locally, no warnings
Public API / website Never raw https — use Fastify / Express / Hono Middleware, routing, error handling, security
Very high-performance microservice Raw https + careful tuning Minimal overhead
Outgoing requests fetch (built-in) Clean, modern, promise-based
Need full control (headers, agents) https.request Fine-grained control

Which direction would you like to go deeper next?

  • Mutual TLS (client certificate authentication)
  • Streaming large files over HTTPS
  • HTTP/2 vs HTTP/1.1 in Node.js
  • TLS configuration for maximum security (ciphers, protocols)
  • Full production setup with redirect + HSTS + secure headers

Just tell me what interests you most — I’ll continue with detailed, copy-paste-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *