简体   繁体   English

Node.js http请求不起作用

[英]Node.js http requests not working

node.js http requests constantly fail on my machine. node.js http请求在我的机器上不断失败。 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. 我有节点0.4.5的这个问题,我之前用0.4.2。 It has serious implication, like npm not working at all. 它有严重的含义,就像npm根本不工作一样。 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 我在2010 Mac Book Pro 15上运行节点并拥有os 10.6.7,就像在同一个wifi路由器上连接的2个同事一样。任何人都知道发生了什么事?有任何机会与任何其他应用程序发生冲突或者在我的机器上运行服务。问候

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM