๐Ÿ” How to Build a Real-World HTTPS Server in Node.js (with Express)

Chauhan BalajiChauhan Balaji
3 min read

How to build a real-world HTTPS server in Node.js(with Express)

If you're building a real web application, you must use HTTPS to protect your usersโ€™ data and secure your APIs.

In this blog, youโ€™ll learn how to set up an HTTPS server in Node.js using Express.js with SSL certificatesโ€”just like itโ€™s done in production.

Letโ€™s dive in! ๐Ÿš€


๐Ÿง  What is HTTPS?

HTTPS (HyperText Transfer Protocol Secure) is the secure version of HTTP. It uses SSL/TLS to encrypt the connection between the client (browser) and the server.

Benefits of HTTPS:

  • ๐Ÿ”’ Secure data transmission

  • ๐Ÿ›ก๏ธ Protects login and payment info

  • ๐Ÿ” Shows a lock icon in browsers

  • ๐Ÿ“ˆ Improves SEO ranking


โš”๏ธ HTTP vs HTTPS โ€“ Whatโ€™s the Difference?

FeatureHTTPHTTPS
๐Ÿ”’ SecurityโŒ Not secureโœ… Encrypted using SSL/TLS
๐Ÿ“ฆ Data ProtectionโŒ Data can be interceptedโœ… Data is encrypted in transit
๐Ÿ” Browser Lock IconโŒ No lock iconโœ… Shows lock icon
๐Ÿšซ Mixed Content Warningsโœ… May trigger warningsโœ… Avoids warnings
๐Ÿ” SEO (Google)โŒ Lower preferenceโœ… Preferred by search engines
๐Ÿงพ Used for PaymentsโŒ Not allowedโœ… Required for secure checkout

๐Ÿ‘‰ In short: Use HTTP only for testing. Use HTTPS in real projects.


๐Ÿ—๏ธ Project Folder Structure

Weโ€™ll organize our project like this:

perlCopyEditmy-secure-app/
โ”œโ”€โ”€ cert/
โ”‚   โ”œโ”€โ”€ cert.pem       # SSL certificate
โ”‚   โ””โ”€โ”€ key.pem        # Private key
โ”œโ”€โ”€ server.js          # Main server file
โ”œโ”€โ”€ package.json

๐Ÿ“ฆ Step 1: Initialize and Install Express

First, create your project:

bashCopyEditmkdir my-secure-app && cd my-secure-app
npm init -y
npm install express

๐Ÿ” Step 2: Generate SSL Certificates (Local Dev Only)

You can create a self-signed certificate using OpenSSL:

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

For production, always use a certificate from a trusted authority like Letโ€™s Encrypt.


๐Ÿง‘โ€๐Ÿ’ป Step 3: Write Your HTTPS Server Code

Create server.js:

jsCopyEditconst fs = require("fs");
const https = require("https");
const express = require("express");
const path = require("path");

const app = express();

// Load SSL Certificate and Private Key
const sslOptions = {
  key: fs.readFileSync(path.join(__dirname, "cert", "key.pem")),
  cert: fs.readFileSync(path.join(__dirname, "cert", "cert.pem")),
};

// Middleware
app.use(express.json());

// Routes
app.get("/", (req, res) => {
  res.send("๐Ÿ” Welcome to your Secure HTTPS Express Server!");
});

app.get("/api", (req, res) => {
  res.json({ message: "This is secure API data" });
});

// Start HTTPS Server
https.createServer(sslOptions, app).listen(4433, () => {
  console.log("๐Ÿš€ HTTPS Server is running at https://localhost:4433");
});

โœ… Step 4: Run the Server

Start the HTTPS server:

bashCopyEditnode server.js

Then go to:

arduinoCopyEdithttps://localhost:4433

Youโ€™ll likely see a browser warning for self-signed certificates. You can safely ignore that during development.


๐Ÿš€ Bonus: Redirect HTTP โ†’ HTTPS (Optional)

Want your app to automatically redirect from HTTP to HTTPS? Here's a simple addition:

jsCopyEditconst http = require("http");

http.createServer((req, res) => {
  res.writeHead(301, { "Location": "https://" + req.headers.host + req.url });
  res.end();
}).listen(8080, () => {
  console.log("๐ŸŒ HTTP server redirecting to HTTPS on port 8080");
});

๐Ÿ Conclusion

Using HTTPS is not optional anymoreโ€”it's a must for every modern website or API. With Node.js and Express, setting up a secure server is actually very easy.

๐Ÿ” Final Tips:

  • Use self-signed certs for local testing only.

  • For production, use Letโ€™s Encrypt (free) or other CA providers.

  • Always redirect HTTP to HTTPS to avoid mixed content issues.

0
Subscribe to my newsletter

Read articles from Chauhan Balaji directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chauhan Balaji
Chauhan Balaji

Computer Science student | Future Software Engineer | Code. Learn. Build. Repeat.