Nodejs HTTP and HTTPS over same port, code not clear
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
add a comment |
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
add a comment |
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
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
node.js http ssl https http-redirect
edited Nov 23 '18 at 9:48
mihai
23.5k73968
23.5k73968
asked Nov 23 '18 at 7:46
nitsnits
345
345
add a comment |
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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