1

I am new to node.js and I am trying to make an http request. I followed a tutorial trying to call random.org. this is my app.js file:

var http = require('http');

//The url we want is: 'www.random.org/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
var options = {
  host: 'www.random.org',
  path: '/integers/?num=1&min=1&max=10&col=1&base=10&format=plain&rnd=new'
};

callback = function(response) {
  var str = '';

  //another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });

  //the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });

  response.on('error', function () {
    console.log("err");
  });
}

http.request(options, callback).end();

So, anyway, when running this file using

node app.js

I get the following error to the console:

C:\Program Files\nodejs>node app.js

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: connect ETIMEDOUT
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)

2 problems:
1. Why am I getting time out error (the site works - I checked)
2. Anyway - why am I not catching this error althought I have an error listener in the response.

Any possible help will be much appreciated!

Thanks

4
  • 1
    The code works in node 0.8 at least. To catch errors on the request http.request().on('error'...) might work. Commented Aug 5, 2013 at 11:25
  • Thanks @AndreasHultgren! You solved me one issue - about the error catching. About version - i am using 0.10.13. Is this matters? Commented Aug 5, 2013 at 11:57
  • Don't think it matters since the request is sent. Try @user568109's suggestions below Commented Aug 5, 2013 at 12:04
  • Have you tried to do the same request with request module? Commented Aug 6, 2013 at 7:19

2 Answers 2

4

You are attaching the error listener to the response, but error happens in request itself. Do someting like this :

http.request(options, callback)
.on('error', function () {
    console.log("err in request");
})
.end();

The error means the site is available, if not the error would show EHOSTUNREACH. ETIMEDOUT means that request was not responded fast enough. It needs some investigation, lke is the network down, try getting www.google.com. What is the timeout value it is considering ? etc.

Sign up to request clarification or add additional context in comments.

2 Comments

Even google returns ETIMEDOUT. I can curl both google and random.org. :(
Does it take some time for curl to show results. In your options add timeout: 10000, (it is in millisec) and try the request.
0

OK. I tried to run this as standalone node js process without browser as background. This is not recommended, and in the cases specified above the problem was that this headerless request lacked cookies.

If I would wrap everything in a createServer method, and upon request I would do the http.request with the same parameters + the cookies from the server request - this would work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.