Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js http requests not working

Tags:

node.js

node.js http requests constantly fail on my machine. I have no idea what is going on. I am running this dead simple script:

var http = require("http");

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'GET'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
req.end();

The request hangs up forever. I am having this problem with node 0.4.5, I had it before with 0.4.2. It has serious implication, like npm not working at all. I am running node on a 2010 Mac Book Pro 15" and have os 10.6.7, just like 2 colleagues connected on the same wifi router. Anyone has an idea of what is going on ? Any chance there is a conflict with any other app or service running on my machine .. Regards

like image 316
rpechayr Avatar asked Nov 15 '22 00:11

rpechayr


1 Answers

Your script works for me, so I'm not sure why it's hanging for you... but, what about creating a client object like so...? Maybe it would help?

var http = require('http');
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/', {'host': 'www.google.com'});
request.on('response', function(response) {
        console.log('STATUS: ' + response.statusCode);
        console.log('HEADERS: ' + JSON.stringify(response.headers));
        response.setEncoding('utf8');
        response.on('data', function(chunk) {
                console.log('BODY: ' + chunk);
        });
});
request.end();
like image 92
sonicwizard Avatar answered Nov 16 '22 17:11

sonicwizard