简体   繁体   English

nodejs https错误:socket与localhost挂起

[英]nodejs https Error: socket hang up with localhost

I have this small code snippet that targets an endpoint hosted on localhost 我有这个小代码片段,目标是在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 } {[错误:套接字挂断]代码:'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. 如果我从浏览器尝试https:// localhost之类的请求,则会成功发送。

If I just change the host in the code to something like encrypted.google.com, it works successfully as well. 如果我只是将代码中的主机更改为encrypted.google.com,它也可以成功运行。

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 编辑 :我也尝试添加浏览器发送的相同标题,如accept,user-agent ..等,但仍然无效

Edit2 : this is the stack trace that appeared when I logged it: Edit2 :这是我记录时出现的堆栈跟踪:

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)

ECONNRESET means the TCP connection was closed unexpectedly, ie. ECONNRESET意味着TCP连接意外关闭,即。 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 也许你遇到了这个问题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 . TL; DR:您可以尝试使用最新的节点版本和secureOptions: constants.SSL_OP_NO_TLSv1_2添加到您的options

UPDATE SSLv3 is broken, https://access.redhat.com/articles/1232123 ; 更新 SSLv3已损坏, https ://access.redhat.com/articles/1232123; maybe ditch ISS? 也许沟渠国际空间站?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM