Nodejs HTTP and HTTPS over same port, code not clear





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















i was going through this code for Nodejs HTTP and HTTPS over same port
link => https://stackoverflow.com/a/23975955



Now, in the code i'm unable to figure out how the proxy is working there



function tcpConnection(conn) {
conn.once('data', function (buf) {
// A TLS handshake record starts with byte 22.
var address = (buf[0] === 22) ? httpsAddress : redirectAddress;
var proxy = net.createConnection(address, function () {
proxy.write(buf);
conn.pipe(proxy).pipe(conn);
});
});
}


i can understand that https server is on 3002 and http on 3001. But when variable address has redirectAddress , it pipes buff into new connection on port of redirectaddress. Then how does it transfers to https ?



I mean port 3000 transfers to 3001 and 3002. but how does 3001 transfers to 3002 ? is it because of http redirect here in this part ?



function httpConnection(req, res) {
var host = req.headers['host'];
res.writeHead(301, { "Location": "https://" + host + req.url });
res.end();
}


I tried on google but couldn't find ans to my doubts.



and also can we not reduce number of ports ? like here 2 additional ports have been used .



Also sometimes out of nowhere the server terminates and i get this error



events.js:182
throw er; // Unhandled 'error' event
^

Error: read ECONNRESET
at _errnoException (util.js:1026:11)
at TCP.onread (net.js:606:25)


what could be the posible reason ?










share|improve this question































    0















    i was going through this code for Nodejs HTTP and HTTPS over same port
    link => https://stackoverflow.com/a/23975955



    Now, in the code i'm unable to figure out how the proxy is working there



    function tcpConnection(conn) {
    conn.once('data', function (buf) {
    // A TLS handshake record starts with byte 22.
    var address = (buf[0] === 22) ? httpsAddress : redirectAddress;
    var proxy = net.createConnection(address, function () {
    proxy.write(buf);
    conn.pipe(proxy).pipe(conn);
    });
    });
    }


    i can understand that https server is on 3002 and http on 3001. But when variable address has redirectAddress , it pipes buff into new connection on port of redirectaddress. Then how does it transfers to https ?



    I mean port 3000 transfers to 3001 and 3002. but how does 3001 transfers to 3002 ? is it because of http redirect here in this part ?



    function httpConnection(req, res) {
    var host = req.headers['host'];
    res.writeHead(301, { "Location": "https://" + host + req.url });
    res.end();
    }


    I tried on google but couldn't find ans to my doubts.



    and also can we not reduce number of ports ? like here 2 additional ports have been used .



    Also sometimes out of nowhere the server terminates and i get this error



    events.js:182
    throw er; // Unhandled 'error' event
    ^

    Error: read ECONNRESET
    at _errnoException (util.js:1026:11)
    at TCP.onread (net.js:606:25)


    what could be the posible reason ?










    share|improve this question



























      0












      0








      0


      1






      i was going through this code for Nodejs HTTP and HTTPS over same port
      link => https://stackoverflow.com/a/23975955



      Now, in the code i'm unable to figure out how the proxy is working there



      function tcpConnection(conn) {
      conn.once('data', function (buf) {
      // A TLS handshake record starts with byte 22.
      var address = (buf[0] === 22) ? httpsAddress : redirectAddress;
      var proxy = net.createConnection(address, function () {
      proxy.write(buf);
      conn.pipe(proxy).pipe(conn);
      });
      });
      }


      i can understand that https server is on 3002 and http on 3001. But when variable address has redirectAddress , it pipes buff into new connection on port of redirectaddress. Then how does it transfers to https ?



      I mean port 3000 transfers to 3001 and 3002. but how does 3001 transfers to 3002 ? is it because of http redirect here in this part ?



      function httpConnection(req, res) {
      var host = req.headers['host'];
      res.writeHead(301, { "Location": "https://" + host + req.url });
      res.end();
      }


      I tried on google but couldn't find ans to my doubts.



      and also can we not reduce number of ports ? like here 2 additional ports have been used .



      Also sometimes out of nowhere the server terminates and i get this error



      events.js:182
      throw er; // Unhandled 'error' event
      ^

      Error: read ECONNRESET
      at _errnoException (util.js:1026:11)
      at TCP.onread (net.js:606:25)


      what could be the posible reason ?










      share|improve this question
















      i was going through this code for Nodejs HTTP and HTTPS over same port
      link => https://stackoverflow.com/a/23975955



      Now, in the code i'm unable to figure out how the proxy is working there



      function tcpConnection(conn) {
      conn.once('data', function (buf) {
      // A TLS handshake record starts with byte 22.
      var address = (buf[0] === 22) ? httpsAddress : redirectAddress;
      var proxy = net.createConnection(address, function () {
      proxy.write(buf);
      conn.pipe(proxy).pipe(conn);
      });
      });
      }


      i can understand that https server is on 3002 and http on 3001. But when variable address has redirectAddress , it pipes buff into new connection on port of redirectaddress. Then how does it transfers to https ?



      I mean port 3000 transfers to 3001 and 3002. but how does 3001 transfers to 3002 ? is it because of http redirect here in this part ?



      function httpConnection(req, res) {
      var host = req.headers['host'];
      res.writeHead(301, { "Location": "https://" + host + req.url });
      res.end();
      }


      I tried on google but couldn't find ans to my doubts.



      and also can we not reduce number of ports ? like here 2 additional ports have been used .



      Also sometimes out of nowhere the server terminates and i get this error



      events.js:182
      throw er; // Unhandled 'error' event
      ^

      Error: read ECONNRESET
      at _errnoException (util.js:1026:11)
      at TCP.onread (net.js:606:25)


      what could be the posible reason ?







      node.js http ssl https http-redirect






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 9:48









      mihai

      24.9k74170




      24.9k74170










      asked Nov 23 '18 at 7:46









      nitsnits

      325




      325
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442542%2fnodejs-http-and-https-over-same-port-code-not-clear%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442542%2fnodejs-http-and-https-over-same-port-code-not-clear%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?