Siva Kannan
Siva Kannan

Reputation: 2481

Asynchronous function throws Error : socket hang up

I am calling a asynchronous function inside a http request function. The called asynchronous function doing another http request,but that http request throws an error Error: socket hang up

code

    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();

In the above code first i am doing a http request to url_1, After requesting i am calling a calling the async function which trying do a http request to url_2, But url_2 http request inside the async function just returns the error which i mentioned above.

Result

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

I am new to node.js, So please excuse if it's a silly question

Upvotes: 0

Views: 2563

Answers (1)

MrVaykadji
MrVaykadji

Reputation: 401

For starter: "www.twitter.com" is 301 (Moved Permanently).

But, as you're using node, you have wonderful node_modules. Why not use them?

Here's what it looks like with got and promises:

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);
});

To install got, simply use:

npm install got

Option 2, if you truly want to use node http module, there's this ugly thing right here:

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();

if you don't .end() your http.request(), it throws a socket hang up error. See Node documentation

Upvotes: 2

Related Questions