2016-03-08 28 views
0

我在一個http請求函數中調用一個異步函數。被叫異步函數做另一個http請求,但http請求引發錯誤Error: socket hang up異步函數拋出錯誤:套接字掛斷

代碼

var http = require("http");  
    var fs = require('fs'); 

    var url_1 = "en.wikipedia.org"; 
    var url_2 = "www.twitter.com"; 

    var options = { host: url_1, port: 80, path: '', method: 'POST'};  
    var api_options = { host:url_2, port: 80,path: '',method: 'POST' }; 


function async(arg, callback) { //async function declaration 
    console.log("received argument "+ arg); 
    http.request(options, function(res){ //http request to url_2 
      console.log("also the second http request successful");//this log not getting printed, insted http.request function in the previous line throws Error : socket hang up 
      return callback(arg); 
    }); 
} 


http.request(options, function(res) { //http request url_1 
      console.log("first http request successful"); 
      async("1",function(return_value){ //calling asynchronous function 
      console.log("count : "+ return_value + " returns callback"); 
      }); 
}).end(); 

在上面的代碼首先我做一個HTTP請求到url_1,請求我後我打電話async函數試圖做一個http請求到url_2,但url_2 http請求裏面的async函數只是返回我上面提到的錯誤。

結果

first http request successful                              
received argument 1                                 
events.js:141                                  
     throw er; // Unhandled 'error' event                           
    ^                                   

Error: socket hang up                                
    at createHangUpError (_http_client.js:203:15)                         
    at Socket.socketOnEnd (_http_client.js:288:23)                         
    at emitNone (events.js:72:20)                             
    at Socket.emit (events.js:166:7)                            
    at endReadableNT (_stream_readable.js:905:12)                         
    at doNTCallback2 (node.js:441:9)                            
    at process._tickCallback (node.js:355:17)                          


Process exited with code: 1 

我是新來的node.js,所以,請原諒,如果這是一個愚蠢的問題

+0

它乞討的問題:在任何情況下,url_2可以正常工作嗎? – Richard

+0

我無法得到你,在任何意義上的條件下? ,你能否詳細點 –

+0

'url_2'很可能會關閉TCP連接,因爲某些原因與你的代碼無關。你能直接調用'url_2'並得到結果嗎?這種錯誤看起來像您的請求格式不正確,以某種方式。 – Richard

回答

2

對於首發: 「www.twitter.com」 是301 (Moved Permanently)

但是,正如你使用節點,你有美妙的node_modules。爲什麼不使用它們?

下面是它看起來像got和承諾:

var request = require('got'); 

var url_1 = "en.wikipedia.org"; 
var url_2 = "google.com"; 

var options = { host: url_1, port: 80, path: '', method: 'POST'};  
var api_options = { host:url_2, port: 80,path: '',method: 'POST' }; 

request(options).then(function (res) { 
    console.log('call1 ok'); 
    return request(api_options); 
}).then(function (res) { 
    console.log('call2 ok'); 
}).catch(function (err) { 
    console.error(err); 
}); 

要安裝got,只需使用:

npm install got 

選項2,如果你真的想使用節點HTTP模塊,這裏有這個醜陋的東西:

var http = require("http"); 

var url_1 = "en.wikipedia.org"; 
var url_2 = "google.com"; 

var options = { host: url_1, port: 80, path: '', method: 'POST'};  
var api_options = { host:url_2, port: 80,path: '',method: 'POST' }; 

var req1, req2; 

req1 = http.request(options, function (res) { 
    res.on('data', function() { 
     console.log('recieved data from req1') 
    }) 
    res.on('end', function() { 
     console.log('call1 ok') 
     req2 = http.request(api_options, function (res2) { 
      res2.on('end', function (body) { 
       console.log('call2 ok') 
      }); 
      res2.on('data', function() { 
       console.log('recieved data from req2') 
      }); 
     }); 
     req2.end(); 
    }); 
}); 
req1.end(); 

如果你沒有.end()你的http.request(),它會拋出一個套接字掛斷錯誤。見Node documentation

+0

它的工作,謝謝 –