'ERR_TOO_MANY_REDIRECTS when using express.js

I have written node.js code, something along the lines of

const express = require("express");

let app = express();
app.use(express.static('views'));
app.use(express.urlencoded({extended: true}));

app.get('/', function(req, res) { 
    res.sendFile("index.html"); 
});

app.post('/site', function(req, res) {
    if (req.body.url) {
        if (/^(?:[a-z]+:)?\/\//i.test(req.body.url)) res.redirect('/redirect/https://'+req.body.url);
        else res.redirect('/redirect/https://'+req.body.url.replace("https://", ""));

        
    } else 
        res.sendFile("no-url.html");


    console.log(req.body);
});


app.listen(3000, () => {
    console.log("Server started on port 3000");
});

When hosting the site using replit, connecting to the site using chromium gives a ERR_TOO_MANY_REDIRECTS error

However, when hosting on localhost, or when not linking the domain on replit, I can connect to the site fine.



Solution 1:[1]

Fixed, it was specifically an issue with cloudflare and I just needed to set SSL/TLS encryption to full

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Roylat Gnail