2013-02-02 42 views
0

當請求被引發到我的應用程序時,我發送一個請求到另一個網絡服務器。但有時,我收到了一些我從未見過的奇怪的解析錯誤。http.ClientRequest錯誤

下面是分析錯誤的樣子:

node.js:201 
     throw e; // process.nextTick error, or 'error' event on first tick 
      ^
Error: Parse Error 
    at Socket.ondata (http.js:1150:24) 
    at TCP.onread (net.js:354:27) 

這是我用來發送請求的代碼:

var request = http.request(options); 
request.end(); 
request.on('response', function (response) { 
    response.on('end', function() { 
     console.log('Response received'); 
    } 
} 

,我有問題是我怎麼能減輕這種所以當這個錯誤發生時我可以再次拋出請求?

+0

你確認你連接的服務器正在返回正確的HTTP數據嗎?也許嘗試連接'telnet'或'nc'並確認? – loganfsmyth

+0

是的。我的瀏覽器似乎也沒有問題。 – TheNotSoWise

回答

0

首先,你應該把你的所有.on()聽衆在你面前.end()一般,因爲不是所有的API nextTick他們.end()方法。

其次,如果您沒有.on('error')偵聽器,則節點的事件發射器會拋出錯誤。所以你的代碼應該看起來更像這樣

var request = http.request(options) 
request.on('response', function (response) { 
    response.on('end', function() { 
    console.log('Response received.') 
    }) 
    response.on('error', function (err) { 
    console.log('I got a response error.') 
    console.error(err.stack) 
    }) 
}) 
request.on('error', function (err) { 
    console.log('I got a request error.') 
    console.error(err.stack) 
}) 
request.end() 
+0

我發佈了這個問題後,我已經發現了'錯誤'偵聽器,但我仍然不確定爲什麼發生這種情況。有沒有辦法訪問http.js並嘗試調查問題? – TheNotSoWise

+0

爲什麼會發生什麼?什麼錯誤發生,或者爲什麼當沒有監聽器時拋出錯誤? –

+0

我會調查'選項',而不是'http' –