繁体   English   中英

nodejs https错误:socket与localhost挂起

[英]nodejs https Error: socket hang up with 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);
});

我总是得到错误:

{[错误:套接字挂断]代码:'ECONNRESET',sslError:undefined}

似乎端点甚至没有收到请求,因为它没有捕获它并且没有发生超时。

如果我从浏览器尝试https:// localhost之类的请求,则会成功发送。

如果我只是将代码中的主机更改为encrypted.google.com,它也可以成功运行。

谁知道为什么会这样呢?

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

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意味着TCP连接意外关闭,即。 在协议中间的某个地方

但是你写的代码对我来说似乎没问题。

也许你遇到了这个问题https://github.com/joyent/node/issues/5360

TL; DR:您可以尝试使用最新的节点版本和secureOptions: constants.SSL_OP_NO_TLSv1_2添加到您的options

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

暂无
暂无

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

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