admin管理员组

文章数量:1208155

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, 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)

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)
Share Improve this question edited Mar 21, 2014 at 5:29 nomier asked Mar 20, 2014 at 21:00 nomiernomier 4121 gold badge3 silver badges12 bronze badges 1
  • can you try strictSSL: false in the options? – Masum Commented Mar 24, 2014 at 20:03
Add a comment  | 

1 Answer 1

Reset to default 19 +150

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?

本文标签: javascriptnodejs https Error socket hang up with localhostStack Overflow