Node.js fetching Twitter Streaming API - EADDRNOTAVAIL

I have the following code written in node.js to access to the Twitter Streaming API. If I curl the URL below, it works. However, I cannot get it to work with my code.

var https = require('https');

https.request('https://USERNAME:PASSWORD@stream.twitter.com/1.1/statuses/sample.json', function(res) {
  res.on('data', function(chunk) {
    var d = JSON.parse(chunk);
    console.log(d);
  });
});

But I receive the following

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: connect EADDRNOTAVAIL
    at errnoException (net.js:642:11)
    at connect (net.js:525:18)
    at Socket.connect (net.js:589:5)
    at Object.<anonymous> (net.js:77:12)
    at new ClientRequest (http.js:1073:25)
    at Object.request (https.js:80:10)
    at Object.<anonymous> (/Users/jordan/Projects/twitter-stream/app.js:3:7)
    at Module._compile (module.js:441:26)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:31)

If anyone can offer an alternative solution, or explain to me why this doesn't work, I would be very grateful.

I recently faced the same problem .Here's the soln

 var https = require('https');   
 var options = {   
   host : 'stream.twitter.com',   
   path : '/1.1/statuses/filter.json?track=bob',    
   method : 'GET',  
   headers : {   
   "Authorization": "Basic "+new Buffer("userName:passwd").toString("base64")
  }   
 };
 var request = https.request(options,function(response){   
   var body = '';
   response.on("data",function(chunk){    
    var tweet = JSON.parse(chunk);  
    console.log(tweet.text);  
  });  
 });  
 request.end();

https.request() wants an options object as its first argument, not a string URL. However, you're probably better off using mikeal's request library, which does take string URLs.

1) as ebohlman already stated https.request takes an options object as first parameter.

http://nodejs.org/api/https.html#https_https_request_options_callback

2) your JSON.parse will most likely fail because the "data" event will be emitted on every chunk and the response could span over more chunks, therefore wait until the response has ended.

var data = ""
res.on('data',function(chunk){
   data+=chunk
})
res.on('end',function(){
  try  {
     var parsed = JSON.parse(data)
  } catch(e) {
     console.log('failed to parse...',e)
  }
})

jst try killing the process node through killall node or do ps -aux|grep node and kill the process through its ID.It will work then.Yeah if you are running on linux.

I faced the same problem, the issue was that I am using an older version of Nodejs (v0.6.19), in that version http.get requires the first parameter as an options object with at least host and path attributes. In the newer versions you can simply give the url string.

http.get({host:'www.google.com',path:'/index.html'}, function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});