我在一個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,所以,請原諒,如果這是一個愚蠢的問題
它乞討的問題:在任何情況下,url_2可以正常工作嗎? – Richard
我無法得到你,在任何意義上的條件下? ,你能否詳細點 –
'url_2'很可能會關閉TCP連接,因爲某些原因與你的代碼無關。你能直接調用'url_2'並得到結果嗎?這種錯誤看起來像您的請求格式不正確,以某種方式。 – Richard