0

I have a basic Express.js application created with express-generator. When I write "localhost:3000" in the browser I want it to add "https://" in front, not "http://", like typing "facebook .com" does. I only create an HTTPS server. The solutions I found on the web either use a proxy server, or create an HTTP server that redirects to HTTPS. Both seem too much for something as simple as defaulting to HTTPS instead of HTTP.

jose
  • 19
  • 5
  • There are no other options that the two. This is because http and https use different ports. If a user uses http, the port 80 is used. Something has then listen there, either a proxy server or a http server in your node code. Otherwise the user will just get nothing from the http request. – Wiktor Zychla Jan 21 '17 at 17:01
  • You HAVE to have a second http server listening that will redirect to https. It's only a few lines of code so I'm not sure why the resistance. If you type `http:/www.google.com` into the browser, the browser issues a request to `http:/www.google.com`. That request returns a 307 redirect to `https://www.google.com` and the browser then requests that https URL. That is how this is done. – jfriend00 Jan 21 '17 at 17:18
  • app.use (function (req, res, next) { if (req.secure) { next(); } else { res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url }); res.end(); } }); – Fernando Rocha Nov 07 '19 at 21:03

0 Answers0