2017-09-15 90 views
0

我已經通過幾個教程和計算器等網站搜索到的問題,但我仍無法弄清楚,爲什麼我的腳本產生錯誤:插座掛斷......插座掛斷節點JS HTTPS請求

希望你們能幫助我 我已經在教程中實施了一個https服務器https://www.pixelstech.net/article/1445603357-A-HTTPS-client-and-HTTPS-server-demo-in-Java

完美工作,客戶端也能正常工作。 但是,當我想在JavaScript來創建一個請求,並與節點JS,我發現了已知的錯誤運行...

我的.js文件:

var https = require('https'); 

var data = JSON.stringify({ 
    firstName: 'JoaquÌn', 
}); 

function getCall() { 
    //initialize options values, the value of the method can be changed to POST to make https post calls 
    var options = { 
     host : 'localhost', 
     port : 9999, 
     path : '/', 
     rejectUnauthorized: false, 
     method : 'POST', 
     headers: {'Connection': 'keep-alive', 
      'Content-Type': 'application/json; charset=utf-8', 
        'Content-Length': Buffer.byteLength(data)} 
    } 

    //making the https get call 
    var getReq = https.request(options, function(res) { 
     console.log("\nstatus code: ", res.statusCode); 
     res.on('data', function(data) { 
      console.log(JSON.parse(data)); 
     }); 
    }); 

    //end the request 
    getReq.end(); 
    getReq.on('error', function(err){ 
     console.log("Error: ", err); 
    }); 
} 

getCall(); 

我的錯誤:

Error: { Error: socket hang up 
    at createHangUpError (_http_client.js:253:15) 
    at TLSSocket.socketOnEnd (_http_client.js:345:23) 
    at emitNone (events.js:91:20) 
    at TLSSocket.emit (events.js:185:7) 
    at endReadableNT (_stream_readable.js:974:12) 
    at _combinedTickCallback (internal/process/next_tick.js:80:11) 
    at process._tickCallback (internal/process/next_tick.js:104:9) code: 'ECONNRESET' } 

的IntelliJ產生當我運行腳本的這一部分:

SSLSession : 
    Protocol : TLSv1.2 
    Cipher suite : TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
Inut : POST/HTTP/1.1 
Inut : Connection: keep-alive 
Inut : Content-Type: application/json; charset=utf-8 
Inut : Content-Length: 24 
Inut : Host: localhost:9999 
Inut : 

我希望你能HEL p我,因爲我不知道爲什麼,我發現了錯誤我已經嘗試了多種解決方案,但它們都沒有工作對我來說...

THX和良好祝願馬丁

回答

0

假設你已經有配置的端口上運行的HTTP服務器,您需要更改host: 'localhost'host: 'http://localhost'

根據已經設置了協議,這既可以是httphttps

+0

嘿:) THX的響應,但是從本地主機轉產到http後本地主機我得到這樣的響應: '錯誤:{錯誤:的getaddrinfo ENOTFOUND的https://本地主機的https://本地主機:9999 在errnoException(dns.js:28:10) 在GetAddrInfoReqWrap.onlookup [按的onComplete](dns.js:76:26) 代碼: 'ENOTFOUND', 錯誤號: 'ENOTFOUND', 系統調用: '的getaddrinfo' , 主機名: 'https://開頭本地主機', 主持人: 'https://開頭本地主機', 端口:9999}' – Kellrkind

+0

你能否把更新的代碼。錯誤表明您正在使用「https」。我假定服務器已經提到的端口 –

+0

嘿對不起,我遲到的響應上運行,但我們已經改變了我們的技術方式,我們正在使用Wildfly服務器(Wildfly羣)與我們的前端:) 通信,但感謝你非常感謝:) – Kellrkind