0

I am using nodejs to handle my server and I have a website on it.

I recently set up SSL and want to redirect http to https but couldn't do it. I tried every approved solution on stackoverflow but none of them are working.

Here is my server app:

const express = require('express');
const app = express();
const https = require('https');
const fetch = require('node-fetch');
const bcrypt = require('bcrypt');
const hogan = require('hogan.js');
const fs = require('fs');

const optionSSL = {
    key: fs.readFileSync("./etc/ssl/myssl.pem"),
    cert: fs.readFileSync("./etc/ssl/myssl.crt")
};

//app.listen(80, () => console.log("Listening at 80"));
app.use(express.static('public', {
    extensions: ['html', 'htm'],
}));

app.use(express.json({limit: '1mb'}));
app.use(express.urlencoded({ extended: false }));

https.createServer(optionSSL, app).listen(443, "mydomain.com");

The things that I tried:

Automatic HTTPS connection/redirect with node.js/express

Redirect nodejs express static request to https

How do you follow an HTTP Redirect in Node.js?

  • 1
    *but couldn't do it*, do you have any errors, in the console or in logs ? Any errors codes ? There is not much we can do without any errors. – Nicolas Jan 14 '20 at 17:58
  • *"I tried every approved solution on stackoverflow but none of them are working."* Please link to the ones you've tried, and include your best effort at the one of them you thought was best/most likely to work. The code in the question shows no attempt at all to do this. – T.J. Crowder Jan 14 '20 at 17:58
  • Actually I got no error on console. When I go to "http:// mydomain" browser can't connect and give me "ERR_CONNECTION_REFUSED" – Kureyş Alp Kılıç Jan 14 '20 at 18:02
  • @T.J.Crowder Edited the post. – Kureyş Alp Kılıç Jan 14 '20 at 18:05
  • I'm not sure about the second parameters of your `listen` function. [according to the doc](https://nodejs.org/api/net.html#net_server_listen) it is supposed to be a callback. – Nicolas Jan 14 '20 at 18:11
  • @Nicolas I can access the website with https:// there is no problem on that but when I type http:// I want to redirect https to access the site. I mean I don't have any problem with this version of my code I just want to when somebody types http:// just redirect it to https:// – Kureyş Alp Kılıç Jan 14 '20 at 18:16
  • Then i think [Quentin's answer](https://stackoverflow.com/a/59739484/5784924) is the right thing to do. – Nicolas Jan 14 '20 at 18:17

2 Answers2

2
https.createServer(optionSSL, app).listen(443, "mydomain.com");

You are listening on port 443, which is the HTTPS port.

If you want to redirect from HTTP then you need to listen on the HTTP port (and not attach certificates to it).

The simplest way to do this would be to run a completely different server (dedicated to performing the redirects) on port 80. You could write one in Node.js, or you could just use something off-the-shelf like nginx or lighttpd.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you for advise. But when I do that how can I add SSL to my website? I can already access https version isn't there any way to add when somebody types http instead of https, redirect it to https? – Kureyş Alp Kılıç Jan 14 '20 at 18:17
  • @KureyşAlpKılıç — See the last two paragraphs of this answer (and note that at no point do I say you should *stop* listening on the HTTPS port). – Quentin Jan 14 '20 at 18:33
  • Actually I am kinda new to these things. I understand from the things that you said is I'll create a new application with nodejs again and run it on a different server so that means double the server price? When someone connecting to my website on some server, actually that someone will be redirected from different server to my actual server. If it is that makes no sense to me I mean it feels like not a proper way to do that. Isn't there something that I can write on my app.js like use different port for http and when someone uses this port redirect that someone to https or something like that – Kureyş Alp Kılıç Jan 14 '20 at 19:29
  • "run it on a different server so that means double the server price?" — No. That **same** server. – Quentin Jan 14 '20 at 19:29
  • "When someone connecting to my website on some server, actually that someone will be redirected from different server to my actual server. " — No. Same server. Just port 80 instead of port 433. – Quentin Jan 14 '20 at 19:29
  • "Isn't there something that I can write on my app.js like use different port for http and when someone uses this port redirect that someone to https or something like that" — Yes, you could write your app to run two servers on two ports. It's just more complicated. Just ignore the last paragraph of the answer if you do that. – Quentin Jan 14 '20 at 19:30
  • `const httpApp = express(); const http = require('http'); httpApp.get("*", function(req, res, next) { res.redirect("https://" + req.header.host + req.path); }); http.createServer(httpApp).listen(80, function() { console.log("Express TTP server listening on port 80"); }); https.createServer(optionSSL, app).listen(443, function() { console.log("Express TTP server listening on port 443"); });` I added this to my app but I got "ERR_CONNECTION_REFUSED" from browser – Kureyş Alp Kılıç Jan 14 '20 at 19:55
0

Here is my solution to this issue:

const httpApp = express();
const http = require('http');

httpApp.get("*", function(req, res, next) {
    res.redirect("https://" + req.headers.host + req.path);
});

http.createServer(httpApp).listen(80, function() {
    console.log("Express TTP server listening on port 80");
});

https.createServer(optionSSL, app).listen(443, function() {
    console.log("Express HTTP server listening on port 443" );
});

Thank you @Quentin for giving me idea of listening on two ports.

zero_cool
  • 3,960
  • 5
  • 39
  • 54