簡體   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