Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Express req.protocol always returns https

I have an server running express. It uses req.protocol to force https. However, when I visit the site on http, it still give https.

Here is the function:

 app.use((req, res, next)=>{
        console.log(req.protocol);
        if(req.protocol === "https"){
            next();
        }else{
            res.redirect(`https://${req.headers.host}${req.url}`);
        }
    });

It isn't redirecting and therefore the site isn't working. What can I do to fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It sounds like your server is only responding to HTTPS requests, not HTTP requests. Remember you serve HTTP and HTTPS on separate ports (80 and 443, by default). From your description, it sounds like you're only serving HTTPS. Here's the example from the Express listen docs for how they serve both:

var express = require('express')
var https = require('https')
var http = require('http')
var app = express()

http.createServer(app).listen(80)
https.createServer(options, app).listen(443)

Note that they called listen twice, once each on two different servers, one for HTTP and another for HTTPS.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!