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
http.request().on('error'...)might work.