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

255
Views
Redirección de HTTP a HTTPS en la aplicación Node y Angular 2

Tengo una aplicación que se ejecuta en el puerto 80 y 443. Cuando el usuario accede a la aplicación de la versión http, debería ser redirigido a HTTPS. He probado el siguiente código para hacer esto.

 function handleRedirects(req, res, next) { if (!req.secure) { return res.redirect('https://' + req.get('host') + req.url); } next(); } app.use(handleRedirects); https.createServer(credentials, app).listen(443, function() { console.log('App is running on port 443'); }); // all other routes are handled by Angular app.get('/*', function(req, res) { console.log("Default handler\n\n\n\n") res.sendFile(path.join(__dirname, '/../../dist/index.html')); }); app.listen(80, function() { logger.info('App is running on port 80'); admins.refresh(); });

Entonces, cuando se inició la aplicación, si presiono localhost , debería ser redirigido a https://localhost . Pero no funciona como se esperaba. ¿Qué hay de malo en el código? He referido la redirección HTTPS para todas las rutas node.js/express - Preocupaciones de seguridad

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Simplemente configuraría https para que solo redirija en un entorno de producción, el código que uso en mi sitio es el siguiente.

 module.exports.httpsRedirect = function(req,res,next){ if(req.headers['x-forwarded-proto'] != 'https' && process.env.NODE_ENV === 'production') res.redirect('https://'+req.hostname+req.url) else next() /* Continue to other routes if we're not redirecting */ };
about 4 years ago · Santiago Trujillo Report

0

El siguiente código resolvió mi problema.

 var app = express(); app.get('/refresh', function (req, res) { res.send(200); }); https.createServer(credentials, app).listen(443, function () { console.log('App is running on port 443'); }); var http = express(); http.get('/', function (req, res) { res.redirect('https://' + req.get('host') + req.url); }) http.listen(80, function () { logger.info('App is running on port 80'); });
about 4 years ago · Santiago Trujillo 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!