Node.js HTTP requests do not work

node.js HTTP requests constantly fail on my machine. I have no idea what is going on. I run 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();

Request hangs forever. I had a problem with node 0.4.5, before I had it with 0.4.2. This has serious consequences, for example, npm does not work at all. I run node in 2010 Mac Book Pro 15 "and has os 10.6.7, just like 2 peers connected to the same Wi-Fi router. Does anyone have an idea of ​​what is happening? Any chance of conflict with any other application or service running on my machine.

+5
source share
1 answer

script , , ... , ...? , ?

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

All Articles