Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs https Error: socket hang up with localhost

I have this small code snippet that targets an endpoint hosted on localhost

var https = require('https');

var options = {
  hostname: 'localhost',
  port: 443,
  path: '/',
  method: 'GET',
  agent: false
};

var req = https.request(options, function(res) {
  console.log("statusCode: ", res.statusCode);
  console.log("headers: ", res.headers);
  res.on('data', function(d) {
    process.stdout.write(d);
  });
});
req.end();

req.on('error', function(e) {
  console.error(e);
});

and I always get the error:

{ [Error: socket hang up] code: 'ECONNRESET', sslError: undefined }

It seems that the request is not even received by the endpoint because the it's not captured by it and there is no timeout happening.

If I try a request like https:// localhost from the browser, it's sent successfully.

If I just change the host in the code to something like encrypted.google.com, it works successfully as well.

Anyone knows why this might happen ?

Edit: I've also tried adding the same headers sent by the browser like accept, user-agent .. etc, but still not working

Edit2: this is the stack trace that appeared when I logged it:

Error: socket hang up
at SecurePair.error (tls.js:1013:23)
at EncryptedStream.CryptoStream._done (tls.js:705:22)
at CleartextStream.read [as _read] (tls.js:496:24)
at CleartextStream.Readable.read (_stream_readable.js:320:10)
at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
at EncryptedStream.g (events.js:180:16)
at EncryptedStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:360:12)
at endWritable (_stream_writable.js:367:3)
at EncryptedStream.Writable.end (_stream_writable.js:345:5)
like image 888
nomier Avatar asked Mar 20 '14 21:03

nomier


People also ask

How do you handle a hang up socket error?

Simply disregard it. This is bolstered by the fact that on such an error, the res socket that your client was listening to is destroyed, despite the fact that it is still editable.

What is socket hang up error in node JS?

Common diagnosis steps The error code [socket hang up][ECONNRESET] indicates that the target server has closed the connection with Edge Microgateway.


1 Answers

ECONNRESET means the TCP connection was closed unexpectedly, ie. somewhere mid protocol.

But the code you wrote seems OK to me.

Maybe you are running into this issue https://github.com/joyent/node/issues/5360

TL;DR: You could try with latest node version and secureOptions: constants.SSL_OP_NO_TLSv1_2 added to your options.

UPDATE SSLv3 is broken, https://access.redhat.com/articles/1232123 ; maybe ditch ISS?

like image 105
wires Avatar answered Sep 30 '22 18:09

wires