Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda