最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to redirect http to https on Node JS - Stack Overflow

programmeradmin1浏览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");

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?

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.");

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?

Share Improve this question edited Jan 14, 2020 at 18:05 Kureyş Alp Kılıç asked Jan 14, 2020 at 17:55 Kureyş Alp KılıçKureyş Alp Kılıç 931 silver badge10 bronze badges 7
  • 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 Commented Jan 14, 2020 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 Commented Jan 14, 2020 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ıç Commented Jan 14, 2020 at 18:02
  • @T.J.Crowder Edited the post. – Kureyş Alp Kılıç Commented Jan 14, 2020 at 18:05
  • I'm not sure about the second parameters of your listen function. according to the doc it is supposed to be a callback. – Nicolas Commented Jan 14, 2020 at 18:11
 |  Show 2 more ments

2 Answers 2

Reset to default 2
https.createServer(optionSSL, app).listen(443, "mydomain.");

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 pletely 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.

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.

发布评论

评论列表(0)

  1. 暂无评论