Nodejs HTTP and HTTPS over same port

前端 未结 4 1329
逝去的感伤
逝去的感伤 2020-11-27 03:58

I\'ve been googling and looking here at stackoverflow, but I can\'t find an answer I like ;-)

I have a NodeJS server that runs over HTTPS and port 3001. Now I\'d lik

4条回答
  •  天命终不由人
    2020-11-27 03:59

    I know its an old question but just putting it as a reference for someone else. The easiest way that I found was to use the https://github.com/mscdex/httpolyglot module. Seems to do what it says quite reliably

        var httpolyglot = require('httpolyglot');
        var server = httpolyglot.createServer(options,function(req,res) {
          if (!req.socket.encrypted) {
          // Redirect to https
            res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
            res.end();
          } else {
            // The express app or any other compatible app 
            app.apply(app,arguments);
          }
      });
     // Some port
     server.listen(11000);
    

提交回复
热议问题