Node.js http requests not working
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 (chun开发者_开发百科k) {
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
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();
精彩评论