nodejs https错误:socket与localhost挂起 [英] nodejs https Error: socket hang up with localhost

查看:306
本文介绍了nodejs https错误:socket与localhost挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个针对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);
});

我总是收到错误:

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

{ [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.

如果我从浏览器尝试https:// localhost这样的请求,它就会成功发送。

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

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

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 ?

编辑:我也尝试添加浏览器发送的相同标头,例如accept,user-agent ..等等,但仍无法正常工作

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

Edit2 :这是我记录时出现的堆栈跟踪:

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)


推荐答案

ECONNRESET 表示TCP连接意外关闭,即。在协议中的某个地方。

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

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

But the code you wrote seems OK to me.

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

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

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

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

UPDATE SSLv3已损坏, https://access.redhat.com/articles/1232123 ;也许沟渠ISS?

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

这篇关于nodejs https错误:socket与localhost挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆