2011-05-04 54 views
2

當使用https.request與node.js的v04.7,我得到以下錯誤:插座掛斷電話時https.request在node.js中

Error: socket hang up 
    at CleartextStream.<anonymous> (http.js:1272:45) 
    at CleartextStream.emit (events.js:61:17) 
    at Array.<anonymous> (tls.js:617:22) 
    at EventEmitter._tickCallback (node.js:126:26) 

簡化代碼,會產生錯誤:

var https = require('https') 
    , fs = require('fs') 

var options = { 
    host: 'localhost' 
, port: 8000 
, key: fs.readFileSync('../../test-key.pem') 
, cert: fs.readFileSync('../../test-cert.pem') 
} 

// Set up server and start listening 
https.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'text/plain'}) 
    res.end('success') 
}).listen(options.port, options.host) 

// Wait a second to let the server start up 
setTimeout(function() { 
    var clientRequest = https.request(options, function(res) { 
    res.on('data', function (chunk) { 
     console.log('Called') 
    }) 
    }) 

    clientRequest.write('') 
    clientRequest.end() 
}, 1000) 

我得到的錯誤,即使在服務器和客戶端上運行不同的節點實例,並與港口8000,3000,443,並使用和不使用SSL證書進行測試。我在我的Ubuntu機器上安裝了libssl和libssl-dev。

任何想法可能是什麼原因?

回答

1

https.createServer(function (req, res) {

當您創建服務器你缺少的選擇,應該是:

https.createServer(options, function (req, res) {

與密鑰和證書內

+0

哇,謝謝@Quentin。很簡單。我有選項哈希定義,但忘了把它傳遞給createServer。 – baalexander 2011-08-09 16:18:14

0

簡短回答:Use the the latest source code而不是你有。將它存儲在你想要的地方,然後需要它,你很好去。


在請求1.2.0源代碼,main.js線76,我看到

http.createClient(options.uri.port, options.uri.hostname, options.uri.protocol === 'https:'); 

綜觀the http.js source code,我看到

exports.createClient = function(port, host) { 
    var c = new Client(); 
    c.port = port; 
    c.host = host; 
    return c; 
}; 

它與3個PARAMS請求但實際功能只有2個。該功能被替換爲https的單獨模塊。

看着the latest main.js source code,我看到了巨大的變化。最重要的是the addition of require('https')

看起來請求已被修復,但從未重新發布。幸運的是,如果您只是從the raw view of the latest main.js source code手動複製並使用它,那麼修復似乎可行。

1

我有一個非常類似的問題,響應的結束事件從未被解僱。

添加這行代碼解決了這一問題:

// Hack to emit end on close because of a core bug that never fires end 
response.on('close', function() {response.emit('end')}); 

我發現在前面的答覆中提到的要求庫的一個例子。

+0

謝謝,但增加了接近事件處理程序不工作。我試着在這一行添加它:https://github.com/baalexander/node-xmlrpc/blob/master/lib/client.js#L63 – baalexander 2011-06-13 19:06:27

0

我有一個類似的問題,我想我有一個修復。但後來我又遇到了一個套接字問題。 看到我的解決方案在這裏:http://groups.google.com/group/nodejs/browse_thread/thread/9189df2597aa199e/b83b16c08a051706?lnk=gst&q=hang+up#b83b16c08a051706

關鍵點:使用0.4.8,http.request而不是http.createClient。

但是,新的問題是,如果我讓程序運行很長時間(我實際上離開程序運行,但在週末沒有活動),那麼當我向http發送請求時,socket會掛起錯誤服務器。 (甚至沒有達到http.request)。我不知道這是否是因爲我的代碼,或者是不同的問題與HTTP服務器