Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda