Redirecting HTTP to HTTPS in Node.js Using Express

Author:Esadax
Updated:May 29, 2025

To ensure that all HTTP traffic is redirected to HTTPS, we can use Express.js middleware along with both http and https modules. This setup will automatically redirect any incoming HTTP requests to HTTPS while allowing HTTPS requests to pass through as normal.

Here's how you can implement it step by step:

  1. Set Up the Express App: Create an instance of Express.js to manage your server logic.

  2. Define Middleware for Redirecting HTTP to HTTPS: Use Express middleware to check each incoming request's protocol. If it's HTTP, redirect it to HTTPS.

  3. Define Your Routes: Specify which URLs should respond with a message or handle other types of traffic.

  4. Create an HTTP Server: Use the http module to create a server that listens on port 3000 and delegates requests to your Express app.

  5. Create an HTTPS Server: Use the https module to create another server that listens on the same port (or a different one if needed) and also delegates requests to your Express app.

  6. Start Both Servers: Begin both servers so they can handle incoming connections.

Here's the complete code:

const express = require('express');
const https = require('https');
const http = require('http');

const app = express();

// Redirect HTTP to HTTPS
app.use((req, res, next) => {
    if (req.protocol === 'http') {
        return res.redirect(`https://${req.hostname}${req.path}`);
    }
    next();
});

// Define routes for HTTPS traffic
app.get('/', (req, res) => {
    res.send('HTTPS is working!');
});

// Create HTTP server
const httpServer = http.createServer((req, res) => {
    app(req, res, next => {
        // Middleware has already been applied here
        // No need to call next() again as middleware handles it
    });
}).listen(3000);

// Create HTTPS server
const httpsServer = https.createServer({ server: app }).listen(3000);

// Start both servers
httpServer.listen(3000);
httpsServer.listen(3000);

Explanation of the Code:

  • Express App Setup: We create an Express app using express() and define its middleware and routes within this instance.

  • Middleware for Redirecting: The middleware function checks if the request's protocol is HTTP. If so, it redirects the client to the same path but over HTTPS.

  • Route Definition: We define a GET route for the root URL (/) which responds with "HTTPS is working!" when accessed via HTTPS.

  • HTTP Server Creation: Using http.createServer(), we create an HTTP server that listens on port 3000. This server delegates each request to the Express app, allowing the middleware and routes to process it.

  • HTTPS Server Creation: Similarly, https.createServer() creates an HTTPS server configured with a certificate (assuming one is already set up). It also listens on port 3000 and delegates requests to the Express app.

  • Starting Servers: Both servers are started using listen(), ensuring they can handle incoming connections and process requests correctly.